UIView TouchsBegan 在动画期间不响应

发布于 2024-10-05 21:22:27 字数 686 浏览 0 评论 0原文

我有一个继承 UIImageView 的可拖动类。当视图没有动画时,拖动效果很好。但在动画时它不会响应触摸。动画完成后,触摸将再次起作用。但我需要它在触摸时暂停动画并在触摸结束时恢复。 我花了一整天的时间研究它,但无法找出原因。

这是我的动画代码。

[UIView animateWithDuration:5.0f 
  delay:0 
  options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) 
  animations:^{ 
  self.center = CGPointMake(160,240);
  self.transform = CGAffineTransformIdentity;
  }
  completion:nil
];

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    NSLog(@"touch");
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    [self.layer removeAllAnimations];
    [[self superview] bringSubviewToFront:self];
}

I have a draggable class that inherits UIImageView. The drag works fine when the view is not animating. But when animating it won't respond to touches. Once the animation is completed the touch works again. But I need it to pause the animation on touches and resume when touch ends.
I spent the whole day researching it but couldn't figure out the reason.

Here is my animation code.

[UIView animateWithDuration:5.0f 
  delay:0 
  options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) 
  animations:^{ 
  self.center = CGPointMake(160,240);
  self.transform = CGAffineTransformIdentity;
  }
  completion:nil
];

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    NSLog(@"touch");
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    [self.layer removeAllAnimations];
    [[self superview] bringSubviewToFront:self];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-10-12 21:22:27

这是因为当动画开始时,ios 将动画视图放置到目标位置,但将其绘制在路径上。因此,如果您在移动时点击视图,您实际上是在点击其框架之外的某个位置。

在动画视图的 init 中,将 userInteractionEnabled 设置为 NO。所以触摸事件是由超级视图处理的。

self.userInteractionEnabled = NO;

在超级视图的touchesBegan方法中,检查动画视图的presentationLayer位置。如果它们与触摸位置匹配,则将 TouchBegan 消息重定向到该视图。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self.view];
    CGPoint presentationPosition = [[animatingView.layer presentationLayer] position];

    if (point.x > presentationPosition.x - 10 && point.x < presentationPosition.x + 10
        && point.y > presentationPosition.y - 10 && point.y < presentationPosition.y + 10) {
        [animatingView touchesBegan:touches withEvent:event];
    }
}

That's because ios places your animating view to the target position, when the animation starts, but draws it on the path. So if you tap the view while moving, you actually tap somewhere out of its frame.

In your animating view's init, set userInteractionEnabled to NO. So the touch events are handled by the superview.

self.userInteractionEnabled = NO;

In your superview's touchesBegan method, check your animating view's presentationLayer position. If they match with the touch position, redirect the touchesBegan message to that view.

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self.view];
    CGPoint presentationPosition = [[animatingView.layer presentationLayer] position];

    if (point.x > presentationPosition.x - 10 && point.x < presentationPosition.x + 10
        && point.y > presentationPosition.y - 10 && point.y < presentationPosition.y + 10) {
        [animatingView touchesBegan:touches withEvent:event];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文