【问题标题】:Detect Touch on an animated View在动画视图上检测触摸
【发布时间】:2012-07-26 04:04:48
【问题描述】:

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

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

首先我用CGAffineTransformTranslation 为它们设置了动画,但后来我意识到如果我用touchesMoved: 检查坐标就不会这样,所以我切换到为 frame 属性设置动画。我很快注意到,帧值在动画期间并没有真正改变,所以我放弃了 touchesEnded: 想法。所以我改成了一个也不起作用的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 不是一种选择。 :(

【问题讨论】:

    标签: iphone cocoa uiview touch uiviewanimation


    【解决方案1】:

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

    我不久前写了a blog post about hit testing animating views/layers,详细解释了这一切。

    【讨论】:

    • 你先生,真棒。很棒的博客。谢谢!
    • 博客没了?
    • 我前段时间移动了我的博客(更新为新链接)并保留了之前的 tumblr 博客。显然它在多年不活动后被删除了。
    • 新博客也被删除并给出 404 错误。但我仍然想知道我是否可以在 Animated.View 中制作可按下的按钮...
    • 我再次修复了断开的链接。
    【解决方案2】:

    当使用 Core Animation 移动任何东西时,对象上的触摸会暂停,直到对象完成移动。有趣的花絮,如果你移动对象并触摸它的最终位置,触摸就会被拾取。这是因为一旦动画开始,对象的帧就被设置为结束点,当它在屏幕上移动时它不会更新。

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

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多