iphone – 如何实现连续拖放菜单效果?
发布时间:2020-12-14 17:21:13 所属栏目:百科 来源:网络整理
导读:我正在尝试实现拖放菜单效果.我不知道如何解决这个问题,也许有人有这种确切影响的经验. 很简单,当用户触摸菜单项时,我希望在他们的触摸位置显示图形.他们的触摸现在将控制图形的平移.在释放触摸后,图形将位于其位置并呈现完整的alpha. 我已经熟悉创建平移手
我正在尝试实现拖放菜单效果.我不知道如何解决这个问题,也许有人有这种确切影响的经验.
很简单,当用户触摸菜单项时,我希望在他们的触摸位置显示图形.他们的触摸现在将控制图形的平移.在释放触摸后,图形将位于其位置并呈现完整的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 *buttonPressed; 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]; buttonPressed = 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:[buttonPressed 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]; } 瞧瞧!连接您的按钮,设置其图像,并在整个屏幕上投放副本. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |