取消 UIView 动画块
下面的代码显示了标签的动画,其中包含用户的状态消息。如果发生事件,标签会显示提示,并通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其更改
为:
原因是当您删除图层的动画时,您将到达此完成块,但
finished
将为false
因为您中断了它。此外,这里的顺序很重要。也许您期望removeAllAnimations
立即调用完成块,但它会在您的showStatusOnLabelWithString:
方法完成后调用,所以发生的情况是您正在调用setHidden:NO
紧接着是setHidden:YES
。Change this:
to this:
The reason is you are reaching this completion block when you remove the animations for the layer, but
finished
will befalse
because you interrupted it. Also, the order is important here. Perhaps you were expectingremoveAllAnimations
to call the completion block instantly, but instead it will be called after yourshowStatusOnLabelWithString:
method finishes, so what is happening is you are callingsetHidden:NO
followed immediately bysetHidden:YES
.您是否尝试从标签层 (
labelStatus.layer
) 中删除动画?Did you try removing animations from the label's layer (
labelStatus.layer
)?