取消 UIView 动画块

发布于 2024-12-29 02:07:18 字数 831 浏览 5 评论 0原文

下面的代码显示了标签的动画,其中包含用户的状态消息。如果发生事件,标签会显示提示,并通过 uiview 动画块慢慢消失。

- (void)showStatusOnLabelWithString:(NSString *)statusMessage
{   
    // [self.view.layer removeAllAnimations]; // not working
    [labelStatus.layer removeAllAnimations]; // not working, too

    [labelStatus setText:statusMessage];
    [labelStatus setHidden:NO];
    [labelStatus setAlpha:1.0];

    [UIView animateWithDuration:5.0 animations:^
     {
         [labelStatus setAlpha:0.0];

     } completion:^(BOOL finished) 
     {
         [labelStatus setHidden:YES];
         [labelStatus setAlpha:1.0];
     }];
}

如果在第一个事件之后的 5 秒内有另一个事件,标签应该再次动画,所以我使用 [self.view.layer removeAllAnimations] 删除了之前的动画(这就是我的想法)。 但标签完全消失,接下来的 5 秒标签又消失了。

如果我(或用户)等待 5 秒,一切都会正常工作。

为什么这不起作用?

亲切的问候, $h@rky

The code below shows an animation of a label which contains a status message for the user. If an event happens the label is showing prompt and is slowly disappearing via uiview animation block.

- (void)showStatusOnLabelWithString:(NSString *)statusMessage
{   
    // [self.view.layer removeAllAnimations]; // not working
    [labelStatus.layer removeAllAnimations]; // not working, too

    [labelStatus setText:statusMessage];
    [labelStatus setHidden:NO];
    [labelStatus setAlpha:1.0];

    [UIView animateWithDuration:5.0 animations:^
     {
         [labelStatus setAlpha:0.0];

     } completion:^(BOOL finished) 
     {
         [labelStatus setHidden:YES];
         [labelStatus setAlpha:1.0];
     }];
}

If there is another event in the following 5s after the first the label should animate again, so I removed the previous animation with [self.view.layer removeAllAnimations] (thats what I thought).
But the label just completely disappear and the next 5s the label is invisible again.

If I (or the user) wait(s) the 5s everything is working properly.

Why isn't this working?

Kind Regards,
$h@rky

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

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

发布评论

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

评论(2

够钟 2025-01-05 02:07:18

将其更改

completion:^(BOOL finished) 
 {
     [labelStatus setHidden:YES];
     [labelStatus setAlpha:1.0];
 }];

为:

completion:^(BOOL finished) 
 {
     if (finished) {
         [labelStatus setHidden:YES];
         [labelStatus setAlpha:1.0];
     }
 }];

原因是当您删除图层的动画时,您将到达此完成块,但 finished 将为 false 因为您中断了它。此外,这里的顺序很重要。也许您期望 removeAllAnimations 立即调用完成块,但它会在您的 showStatusOnLabelWithString: 方法完成后调用,所以发生的情况是您正在调用 setHidden:NO 紧接着是 setHidden:YES

Change this:

completion:^(BOOL finished) 
 {
     [labelStatus setHidden:YES];
     [labelStatus setAlpha:1.0];
 }];

to this:

completion:^(BOOL finished) 
 {
     if (finished) {
         [labelStatus setHidden:YES];
         [labelStatus setAlpha:1.0];
     }
 }];

The reason is you are reaching this completion block when you remove the animations for the layer, but finished will be false because you interrupted it. Also, the order is important here. Perhaps you were expecting removeAllAnimations to call the completion block instantly, but instead it will be called after your showStatusOnLabelWithString: method finishes, so what is happening is you are calling setHidden:NO followed immediately by setHidden:YES.

在巴黎塔顶看东京樱花 2025-01-05 02:07:18

您是否尝试从标签层 (labelStatus.layer) 中删除动画?

Did you try removing animations from the label's layer (labelStatus.layer)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文