当我在 TouchBegan 内部设置动画时,触摸丢失

发布于 2024-10-20 07:23:39 字数 490 浏览 2 评论 0原文

我想在屏幕上有 4 个 UIView,并在触摸时为其背景颜色设置动画,并在触摸结束时将其更改回来。

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    [UIView animateWithDuration:0.1
        animations:^{
            self.backgroundColor = altColor;
        }];
}

不幸的是,当我在touchesBegan中执行此动画时,除非动画完成后触摸结束,否则touchesEnded不会触发。这意味着在动画过程中,touchesEnded 会丢失触摸。

此外,快速点击 UIView 不会导致多次 TouchBegan 调用。似乎在动画(在第一次触摸中生成)完成之前不会收到新的触摸。

如何用动画响应所有触摸?我实际上希望每次新的触摸都会中断之前的动画......并重新开始。

I'd like to have 4 UIViews on the screen and animate their background color when touched and change it back when touches end.

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    [UIView animateWithDuration:0.1
        animations:^{
            self.backgroundColor = altColor;
        }];
}

Unfortunately, when I perform this animation in touchesBegan - touchesEnded won't fire unless the touch ended after the animation completed. This means that touchesEnded gets lost for touches during the animation.

In addition, rapidly hitting the UIView doesn't result in multiple touchesBegan invocations. It seems that no new touches are received until the animation (spawned in the first touch) completes.

How can I respond to all touches with animations? I'd actually like each new touch to interrupt the previous animation ... and start it over again.

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

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

发布评论

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

评论(2

贵在坚持 2024-10-27 07:23:39

看起来我需要在块动画上设置一个选项

options:UIViewAnimationOptionAllowUserInteraction

Looks like I need to set an option on the block animation

options:UIViewAnimationOptionAllowUserInteraction
情深缘浅 2024-10-27 07:23:39

事实证明,这种旧类型的动画似乎并没有阻止新的touchesBegan或touchesEnded:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    [UIView beginAnimations:@"touchesBegan" context:nil];
    self.backgroundColor = altColor;
    [UIView commitAnimations];
}

Turns out that this older type of animation doesn't seem to block new touchesBegan or touchesEnded:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    [UIView beginAnimations:@"touchesBegan" context:nil];
    self.backgroundColor = altColor;
    [UIView commitAnimations];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文