gpt4 book ai didi

iphone - 如何实现连续拖拽菜单效果?

转载 作者:可可西里 更新时间:2023-11-01 06:12:15 28 4
gpt4 key购买 nike

我正在尝试实现拖放菜单效果。我不确定该怎么做,也许有人对这种确切效果有经验。

很简单,当用户触摸菜单项时,我希望在他们的触摸位置出现一个图形。他们的触摸现在将控制图形的平移。释放触摸后,图形将位于其位置并呈现完整的 alpha。

我已经熟悉创建平移手势和实例化图形。到目前为止,我可以创建触摸菜单项的图形。最大的问题是我如何“跳过”触摸手势,使其成为一个连续的 Action 。

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

感谢任何帮助。谢谢 enter image description here

最佳答案

我玩这个很开心。以下代码将在触摸时从按钮抓取图像,将图像拖动到 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

现在在你的主视图 Controller 中,添加:

#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 将是您的 apple 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];
}

等等!连接你的按钮,设置它的图像,然后将副本扔到整个屏幕上。 :)

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

干杯!

编辑:修复了 -(void)draw:(UIImage *)image{} 以便正确调整图像大小。

关于iphone - 如何实现连续拖拽菜单效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8812195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com