ios - 实现类似行为的应用程序图标

标签 ios iphone objective-c

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

  1. 长按时摇摆并显示删除图标
  2. 按删除图标并删除 View 。

通过这个代码人,我可以正确地获得第一名。

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;
        }];
    }
}

现在,我希望 deletePressed 在 View 不稳定且删除按钮被按下时被触发。但这永远不会触发。请帮我找出问题所在。

最佳答案

我为此伤透了脑筋,因为它对我来说没有意义。然后我注意到代码在 UIView animateWithDuration block 之前一直运行良好。

显然 还有一个 UIViewAnimationOptionAllowUserInteraction 选项,默认设置为 NO。将您的代码更改为此,您应该没问题:

[UIView animateWithDuration:0.2 delay:0 options:( UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction) animations:^{
            view.transform = rightWobble;
        }completion:^(BOOL finished){
            view.transform = CGAffineTransformIdentity;
        }];

此外,如果它仍然不起作用并且您的 View 是 UIImageView,则设置 view.userInteractionEnabled = YES

我的工作代码如下所示: totalcode

关于ios - 实现类似行为的应用程序图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24401632/

相关文章:

iphone - ios:在iPhone应用程序中设计购物车

ios - NSSQLite 迁移到 Seam3 存储类型失败并出现 EXC_BAD_ACCESS

iphone - 如何在 iOS 5 中后台向收件人发送电子邮件?

iphone - iOS - 使用 UIPanGestureRecognizer 平移时实时推进动画

ios - 不兼容的 block 指针 SKSpriteNode

ios - 如何让振动设备保持在后台

ios - 如何将两个m4a音频文件一起 “Mix/Superimpose”

iphone - 从异步 url 连接返回数据给调用者

objective-c - 如何计算frame.size.width

c++ - Box2D/OpenGL : one texture mapped on many vertices