很简单,当用户触摸菜单项时,我希望在他们的触摸位置显示图形.他们的触摸现在将控制图形的平移.在释放触摸后,图形将位于其位置并呈现完整的Alpha.
我已经熟悉创建平移手势和实例化图形.到目前为止,我可以创建触摸菜单项的图形.最大的问题是我如何“越过”触摸手势,这是一个单一的连续动作.
此外,菜单项应该是UIbutton还是UIImageVIEw?
任何帮助赞赏.谢谢
解决方法 我对这个玩得很开心.以下代码将在触摸时从按钮中抓取图像,将该图像拖动到Alpha = 0.5,并在您的触摸以Alpha = 1.0结束时将其放下.此后它将继续拖延.导入QuartzCore后,创建一个新文件. .h应该是:
#import <Foundation/Foundation.h>#import <QuartzCore/CAGradIEntLayer.h>#import <QuartzCore/CALayer.h>@interface DraggableImage : CAGradIEntLayer- (voID)draw:(UIImage *)image;- (voID)movetoFront;- (voID)appearDraggable;- (voID)appearnormal;@end
并且.m应该是:
#import "DraggableImage.h"@implementation DraggableImage- (voID)draw:(UIImage *)image{ CGRect buttonFrame = self.bounds; int buttonWIDth = buttonFrame.size.wIDth; int buttonHeight = buttonFrame.size.height; UIGraphicsBeginImageContext( CGSizeMake(buttonWIDth,buttonHeight) ); [image drawInRect:self.bounds]; UIImage* newImage = UIGraphicsGetimageFromCurrentimageContext(); UIGraphicsEndImageContext(); [newImage drawInRect:self.bounds];}- (voID)movetoFront { CALayer *superlayer = self.superlayer; [self removeFromSuperlayer]; [superlayer addSublayer:self];}- (voID)appearDraggable { self.opacity = 0.5;}- (voID)appearnormal { self.opacity = 1.0;}@end
现在在主视图控制器中,添加:
#import <UIKit/UIKit.h>#import <QuartzCore/QuartzCore.h>#import "DraggableImage.h"@interface YourVIEwController : UIVIEwController{ DraggableImage *heldImage; DraggableImage *imageForFrame[5]; // or however many UIbutton *button@R_502_4848@; int imageCount;}@property (weak,nonatomic) IBOutlet UIbutton *imagebutton;-(IBAction)buildImageLayerForbutton:(UIbutton *)sender;- (voID)moveHeldImagetoPoint:(CGPoint)location;- (CALayer *)layerFortouch:(UItouch *)touch;
在这种情况下,imagebutton将是你的苹果按钮.现在在.m文件中,添加以下内容:
@synthesize imagebutton;#pragma - mark touches-(voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ CALayer *hitLayer = [self layerFortouch:[touches anyObject]]; if ([hitLayer isKindOfClass:[DraggableImage class]]) { DraggableImage *image = (DraggableImage *)hitLayer; heldImage = image; [heldImage movetoFront]; } hitLayer = nil; [super touchesBegan:touches withEvent:event];}-(voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ if (heldImage) { UItouch *touch = [touches anyObject]; UIVIEw *vIEw = self.vIEw; CGPoint location = [touch locationInVIEw:vIEw]; [self moveHeldImagetoPoint:location]; }}- (voID) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if (heldImage) { [heldImage appearnormal]; heldImage = nil; }}- (voID)dragBegan:(UIControl *)c withEvent:ev {}- (voID)dragMoving:(UIControl *)c withEvent:ev { UItouch *touch = [[ev alltouches] anyObject]; CGPoint touchPoint = [touch locationInVIEw:self.vIEw]; [self moveHeldImagetoPoint:touchPoint];}- (voID)dragEnded:(UIControl *)c withEvent:ev { UItouch *touch = [[ev alltouches] anyObject]; CGPoint touchPoint = [touch locationInVIEw:self.vIEw]; [self moveHeldImagetoPoint:touchPoint]; [heldImage appearnormal]; heldImage = nil;}-(IBAction)buildImageLayerForbutton:(UIbutton *)sender{ DraggableImage *image = [[DraggableImage alloc] init]; button@R_502_4848@ = sender; CGRect buttonFrame = sender.bounds; int buttonWIDth = buttonFrame.size.wIDth; int buttonHeight = buttonFrame.size.height; image.frame = CGRectMake(120,24,buttonWIDth*3,buttonHeight*3); image.backgroundcolor = [UIcolor lightGraycolor].CGcolor; image.delegate = self; imageForFrame[imageCount] = image; [self.vIEw.layer addSublayer:image]; [image setNeedsdisplay]; [image movetoFront]; [image appearDraggable]; heldImage = image; [self moveHeldImagetoPoint:sender.center]; imageCount++;}- (voID)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { UIGraphicsPushContext(ctx); DraggableImage *image = (DraggableImage *)layer; [image draw:[button@R_502_4848@ imageForState:UIControlStatenormal]]; UIGraphicsPopContext();}- (voID)moveHeldImagetoPoint:(CGPoint)location { float dx = location.x; float dy = location.y; CGPoint newposition = CGPointMake(dx,dy); [CATransaction begin]; [CATransaction setdisableActions:TRUE]; heldImage.position = newposition; [CATransaction commit];}- (CALayer *)layerFortouch:(UItouch *)touch { UIVIEw *vIEw = self.vIEw; CGPoint location = [touch locationInVIEw:vIEw]; location = [vIEw convertPoint:location toVIEw:nil]; CALayer *hitPresentationLayer = [vIEw.layer.presentationLayer hitTest:location]; if (hitPresentationLayer) { return hitPresentationLayer.modelLayer; } return nil;}-(voID)vIEwDIDLoad{ [imagebutton addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventtouchDown]; [imagebutton addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventtouchDragInsIDe | UIControlEventtouchDragOutsIDe]; [imagebutton addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventtouchUpInsIDe | UIControlEventtouchUpOutsIDe]; [super vIEwDIDLoad];}- (voID)vIEwDIDUnload { [self setimagebutton:nil]; [super vIEwDIDUnload];}
瞧瞧!连接您的按钮,设置其图像,并在整个屏幕上投放副本. 总结
以上是内存溢出为你收集整理的iphone – 如何实现连续拖放菜单效果?全部内容,希望文章能够帮你解决iphone – 如何实现连续拖放菜单效果?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)