【问题标题】:Detecting if a moving UIImageView has been touched检测移动的 UIImageView 是否被触摸
【发布时间】:2012-05-14 12:52:35
【问题描述】:

我有一个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) {

    }];    
}

【问题讨论】:

  • 你是在继承 UIImageView 吗?因为否则您无法访问 touchesBegan 委托方法。另外,你为什么不简单地使用 UIGestureRecongizers?
  • 另一种方法是使用自定义按钮并将图像作为该按钮的背景图像,然后您可以轻松理解是否触摸它
  • 我对编程很陌生,之前没有使用过 UIGestureRecognizers。在开始使用触摸之前,我决定这样做。以下答案似乎对我有用

标签: iphone ios uiimageview touchesbegan


【解决方案1】:

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

参见 animateWithDuration:delay:options:animations:completion: 文档:

在动画期间,用户交互会暂时禁用动画视图。 (在 iOS 5 之前,整个应用程序都禁用了用户交互。)如果您希望用户能够与视图交互,请在 options 参数中包含 UIViewAnimationOptionAllowUserInteraction 常量。

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

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

【讨论】:

  • @garethdn:如果 J_D 有效,您应该接受它的回答 - 它应该有效 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多