Ios 实现类似应用图标的行为

Ios 实现类似应用图标的行为,ios,iphone,objective-c,Ios,Iphone,Objective C,我想实现一个应用程序图标(在主屏幕上)一样的行为。所以我需要两个功能 长按时摆动并显示删除图标 按delete(删除)图标并删除视图 我可以通过这个代码的人正确地得到1号 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [longPress setMinimumPressDur

我想实现一个应用程序图标(在主屏幕上)一样的行为。所以我需要两个功能

  • 长按时摆动并显示删除图标
  • 按delete(删除)图标并删除视图
  • 我可以通过这个代码的人正确地得到1号

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [longPress setMinimumPressDuration:1];
        [view addGestureRecognizer:longPress];
    
    - (void)longPress:(UILongPressGestureRecognizer*)gesture {
        if ( gesture.state == UIGestureRecognizerStateBegan ) {
            CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5));
            CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5.0));
            view.transform = leftWobble;  // starting point
    
    
            UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
            deleteButton.frame = CGRectMake(-4, 0, 43, 54);
            [view addSubview: deleteButton];
            [view bringSubviewToFront:deleteButton];
            [deleteButton setBackgroundImage:[UIImage imageNamed:@"trashcan.png"] forState:UIControlStateNormal];
            [deleteButton addTarget:self action:@selector(deletePressed:) forControlEvents:UIControlEventTouchUpInside];
    
            [UIView animateWithDuration:0.5 delay:0 options:( UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut) animations:^{
                view.transform = rightWobble;
            }completion:^(BOOL finished){
                view.transform = CGAffineTransformIdentity;
            }];
        }
    }
    

    现在我希望当视图不稳定并且按下delete按钮时,deletePressed会被触发。但这永远不会触发。请帮我找出哪里出了问题。

    我为这件事伤了脑筋,因为这对我来说毫无意义。然后我注意到代码在
    UIView animateWithDuration
    块之前一直在工作

    显然还有一个默认设置为“否”的
    UIViewAnimationOptionAllowUserInteraction
    选项。将您的代码更改为此,您应该可以:

    [UIView animateWithDuration:0.2 delay:0 options:( UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction) animations:^{
                view.transform = rightWobble;
            }completion:^(BOOL finished){
                view.transform = CGAffineTransformIdentity;
            }];
    
    另外,如果它仍然不起作用,并且您的视图是UIImageView,则设置
    view.userInteractionEnabled=YES

    我的工作代码如下所示:

    在提供动画时,应添加用户交互选项

    [UIView animateWithDuration:0.5 delay:0 options:
         (UIViewAnimationOptionAutoreverse|
          UIViewAnimationOptionRepeat|
          UIViewAnimationOptionCurveEaseInOut| 
          UIViewAnimationOptionAllowUserInteraction) 
          animations:^{
                view.transform = rightWobble;
                }completion:^(BOOL finished){
                     view.transform = CGAffineTransformIdentity;
                }];
    
    这是因为摇摆效应