检测移动的 UIImageView 是否已被触摸



我有一个处于动画循环中的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) {
    }];    
}

这可能是因为在动画期间默认禁用用户集成。

请参阅动画持续时间:延迟:选项:动画:完成: 文档:

在动画期间,暂时禁用要设置动画的视图的用户交互。(在 iOS 5 之前,将禁用整个应用程序的用户交互。如果希望用户能够与视图交互,请在选项参数中包含 UIViewAnimationOptionAllowUserInteraction 常量。

http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html1

您可能只需将UIViewAnimationOptionAllowUserInteraction添加到传递给方法"beeBobbing"中的选项的值中即可。

最新更新