我是ios和核心动画的新手,所以我正在做一些测试以适应它。
我已经尝试通过长按手势来创建菜单。我希望按钮先设置动画,然后再设置动画,然后消失。我让外观部分起作用了,但是我不知道如何使它消失。它还不允许我在UIGestureRecognizerStateEnded语句中为imageView设置动画。基本上,我有两个问题:

  • 释放长按时,如何为按下的按钮设置动画?
  • 如何仅使这些按钮之一而不是每次长按都出现?

  • 这是我在.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
    

    最佳答案

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

    @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");
                }];
        }
    }
    

    让我知道您是否需要更多帮助。

    关于ios - 长按手势弹出菜单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21792216/

    10-11 12:18