用长按手势弹出菜单

时间:2014-02-15 01:20:01

标签: ios objective-c

我是ios和核心动画的新手,所以我正在做一些测试以适应它。 我有一个菜单,我试图通过长按手势创建。我希望按钮通过动画制作然后动画下来然后消失。我让出现的部分正常工作,但我无法弄清楚如何让它消失。它也不允许我在我的UIGestureRecognizerStateEnded语句中为我的imageView设置动画。基本上,我有两个问题:

  1. 如何在长按被释放后为按钮设置动画?

  2. 每次长按时,我怎样才能制作其中一个按钮而不是它们?

  3. 这就是我的.m

    中的内容
    -(void)onPress:(UILongPressGestureRecognizer*)longpress{
    
    CGPoint touchCenter = [longpress locationInView:self.view];
    
    UIImage *plus = [UIImage imageNamed:@"plus"];
    imageView = [[UIImageView alloc]initWithImage:plus];
    
    CGRect imageViewFrame = CGRectMake(0, 0, 35, 35);
    imageViewFrame.origin = CGPointMake(touchCenter.x - imageViewFrame.size.width / 2.0f, touchCenter.y - imageViewFrame.size.height / 2.0f);
    imageView.frame = imageViewFrame;
    
    if (longpress.state == UIGestureRecognizerStateBegan) {
        [self.view addSubview:imageView];
    
        [UIView animateWithDuration:0.6 animations:^{
            CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, -40);
            imageView.transform = moveTrans;
        }];
        NSLog(@"Long press");
        return;
    }else{
    
    
    if (longpress.state == UIGestureRecognizerStateEnded || longpress.state == UIGestureRecognizerStateCancelled || longpress.state == UIGestureRecognizerStateFailed) {
    
        [UIView animateWithDuration:0.6 animations:^{
            CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, 40);
            imageView.transform = moveTrans;
            NSLog(@"long press done");
        }];
    
    }
    
    
    }
    

    }

    和我的.h

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    
    @interface ViewController : UIViewController<UIGestureRecognizerDelegate>
    {
        UIImageView *imageView;
        CAShapeLayer *layer;
    
    }
    @property(nonatomic) float angle;
    @property(nonatomic, strong) UIImageView *imageView;
    @property (nonatomic, strong) UIImage *image;
    @end
    

1 个答案:

答案 0 :(得分:2)

我现在确定你想做什么。但我修改了你的一些代码:

@interface ViewController ()
@property (strong, nonatomic) UIView *moveView;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILongPressGestureRecognizer *recoginzer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onPress:)];
    [self.view addGestureRecognizer:recoginzer];
}

- (void)onPress:(UILongPressGestureRecognizer*)longpress {
    CGPoint location = [longpress locationInView:self.view];
    if (longpress.state == UIGestureRecognizerStateBegan) {
        if (!self.moveView) {
            self.moveView = [[UIView alloc] initWithFrame:CGRectMake(location.x, location.y, 100, 100)];
            self.moveView.backgroundColor = [UIColor redColor];
            [self.view addSubview:self.moveView];
        } else {
            self.moveView.frame = CGRectMake(location.x, location.y, 100, 100);
        }
        [UIView animateWithDuration:0.6 animations:^{
            CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, -40);
            self.moveView.transform = moveTrans;
        }];
        NSLog(@"Long press");
    } else if (longpress.state == UIGestureRecognizerStateEnded || longpress.state == UIGestureRecognizerStateCancelled || longpress.state == UIGestureRecognizerStateFailed) {
            [UIView animateWithDuration:0.6 animations:^{
                CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(0, 40);
                self.moveView.transform = moveTrans;
                NSLog(@"long press done");
            }];
    }
}

如果您需要更多帮助,请与我联系。