iphone - 检测移动的 UIImageView 是否被触摸

标签 iphone ios uiimageview touchesbegan

我有一个位于动画循环中的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/

相关文章:

iPhone:在运行时下载 zip 并在主包子目录中解压

iOS 8 UITableView inset : if you have cells with custom layout, 你可能也会有这个问题

iphone - 如何使 UITextView 文本全部可选并向用户 iPhone 应用程序显示复制选项?

ios - 当我使用 CoreData 时,出现错误 : [NSAsynchronousFetchResult count]: unrecognized selector sent to instance

ios - UIImageview 未缩放

ios - Swift 跳过一个动画 (UIView.animate)

iphone - AFNetworking with CoreData 线程安全吗?

ios - Signed Hex 到 Signed Int Swift

ios - Xcode 自动建议和索引问题

ios - 如何在 iPhone 中将 ImageView 的位置设置为 UITableViewCell 的中心