我有一个位于动画循环中的UIImageView
。我想检测它是否被触摸并用 NSLog
打印一条消息。我们的想法是,如果它被触摸,则执行另一个动画,但目前我无法检测到它是否首先被触摸。用户交互已启用。这是代码:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
UIView *touchedView = [touch view];
if (touchedView == imgSun) {
NSLog(@"Sun touched");
[self spinSun];
} else if (touchedView == imgBee) {
NSLog(@"Bee touched");
} else if (touchedView == imgClouds) {
NSLog(@"Clouds touched");
}
}
动画方法:
-(void) beeBobbing
{
[UIView animateWithDuration:1.0f delay:0 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
CGPoint bottomPoint = CGPointMake(215.0, 380.0);
imgBee.center = bottomPoint;
} completion:^(BOOL finished) {
}];
}
最佳答案
这可能是因为在动画期间默认禁用用户集成。
请参阅 animateWithDuration:delay:options:animations:completion: 文档:
在动画期间,对于正在动画的 View ,用户交互会暂时禁用。 (在 iOS 5 之前,整个应用程序都禁用用户交互。)如果您希望用户能够与 View 交互,请在 options 参数中包含 UIViewAnimationOptionAllowUserInteraction 常量。
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html 1
您可以将 UIViewAnimationOptionAllowUserInteraction 添加到传递给方法“beeBobbing”中的选项的值。
关于iphone - 检测移动的 UIImageView 是否被触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10583722/