在动画视图上检测触摸

时间:2012-07-27 13:12:03

标签: iphone cocoa uiview touch uiviewanimation

我正在使用animateWithDuration:设置几个视图的动画,而我根本无法检测到它们的任何触摸。

我尝试过简单的触摸处理(touchesEnded:)和tapGestureRecognizer

首先我用CGAffineTransformTranslation给它们制作了动画,但后来我意识到,如果我用touchesMoved:检查坐标,我就不会这样做,所以我切换到框架属性的动画。我很快注意到,在动画期间帧值并没有真正改变,所以我放弃了touchesEnded:idea。所以我改为tapGestureRecognizer,它也不起作用。

我在所有视图上都启用了userInteraction,并且还为动画添加了选项UIViewAnimationOptionAllowUserInteraction

以下是动画的代码和之前发生的事情:

// init the main view
singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnItem:)]; // singleFingerTap is an ivar

// code somewhere:
// userItem is a subview of MainView
[userItem addGestureRecognizer:singleFingerTap];

[UIView animateWithDuration:animationTime
                      delay:0.0f
                    options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     userItem.frame = CGRectMake(-(self.bounds.size.width + userItem.frame.size.width), userItem.frame.origin.y, userItem.frame.size.width, userItem.frame.size.height);
                 }

                 completion:^(BOOL finished) {
                     if (finished) {
                         [unusedViews addObject:userItem];
                         [userItem removeFromSuperview];
                         [userItem removeGestureRecognizer: singleFingerTap];
                     }
                 }
 ];

这是手势识别器:

- (void) handleTapOnItem:(UITapGestureRecognizer *)recognizer{
   NSLog(@"Touched");
}

那么我怎样才能真正从动画视图中获得触摸或者什么是最佳解决方案? 不能在每个视图中添加透明的uibutton。 :(

2 个答案:

答案 0 :(得分:3)

将动画应用于视图时,动画属性会立即更改为其结束值。您在屏幕上实际看到的是视图层的表示层。

我前一段时间写了a blog post about hit testing animating views/layers,更详细地解释了这一切。

答案 1 :(得分:0)

当使用Core Animation移动任何内容时,对象的触摸将停止,直到对象完成其移动。有趣的tid-bit,如果您移动对象并触摸其最终位置所在的位置,则会拾取触摸。这是因为一旦动画启动,对象的帧就会被设置为结束点,当它在屏幕上移动时不会更新。

您可能需要研究Quartz或OpenGL ES等替代技术来检测视图移动时的触摸。

相关问题