我想实现一个应用程序图标(在主屏幕上)之类的行为。所以我需要两个功能。
- 长按时摇摆并显示删除图标
- 按删除图标并删除 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
我的工作代码如下所示:
关于ios - 实现类似行为的应用程序图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24401632/