如何实现连续拖放菜单效果?

我试图实现一个拖放菜单影响。 我不知道如何去做,也许有人有这个确切的效果的经验。

很简单,当用户触摸一个菜单项,我想要一个graphics出现在他们的触摸位置。 他们的触摸现在将控制graphics的平移。 一旦释放触摸,graphics将坐在原处,并呈现完整的阿尔法。

我已经熟悉创build平移手势和实例化graphics。 到目前为止,我可以创build菜单项被触摸的graphics。 最大的问题是我如何“通过”触摸手势,因此它是一个单一的和连续的运动。

另外,应该菜单项是UIButton或UIImageView?

任何帮助赞赏。 谢谢 在这里输入图像说明

我对这个有一些乐趣。 下面的代码会在触摸时从button中抓取图像,在alpha = 0.5的位置拖动图像,然后放在alpha = 1.0的地方。 此后它将继续拖延。

导入QuartzCore后,创build一个新的文件。 .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将是你的苹果Button。 现在在你的.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]; } 

Et瞧! 连接你的button,设置其图像,并抛出整个屏幕上的副本。 🙂

注:我没有太多评论,但会很乐意回答任何问题。

干杯!

编辑:固定-(void)draw:(UIImage *)image{}以便它将正确调整图像的大小。

如果你想要将触摸function传递给第二个graphics(大),我认为你可以做这样的事情

在.h你必须声明图像,你要拖动和浮动variables记住可拖动button的前一点(我假设你使用IOS 5 SDK)

 @property(nonatomic, strong) UIImageView* myImage; @property float pointX; @property float pointY; 

那么,在.m你可以做到这一点

 myImage = [[UIImageView alloc]initWithImage:@"appleImage.jpg"]; myImage.alpha = 0; //default UIImageView interaction is disabled, so lets enabled it first myImage.userInteractionEnabled = YES; [button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 

然后进行拖拽function

 - (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event { self.myImage.alpha = 0.5; UITouch *touch = [[event touchesForView:button] anyObject]; CGPoint previousLocation = [touch previousLocationInView:button]; CGPoint location = [touch locationInView:button]; CGFloat delta_x = location.x - previousLocation.x; CGFloat delta_y = location.y - previousLocation.y; // move button, to keep the dragging effect button.center = CGPointMake(button.center.x + delta_x, button.center.y + delta_y); // moving the image button.center = CGPointMake(button.center.x + delta_x, button.center.y + delta_y); self.pointX = previousLocation.x; self.pointY = previousLocation.y; [button addTarget:self action:@selector(dragRelease:withEvent:) forControlEvents:UIControlEventTouchUpInside]; } 

最后,将dragRelease函数放到你将button返回到原来的位置,并将图像的alpha值设置为1

 -(void)dragRelease:(UIButton *)button withEvent:(UIEvent *)event { self.myImage.alpha = 1; button.center = CGPointMake(pointX, pointY); } 

你完成了:3

这只是基本的想法,也许这不是你想要的,但我希望这有助于

编辑*:哦,不要忘记综合所有的属性,如果你使用低于5.0的SDK,你可以改变“强”属性“保留”