gpt4 book ai didi

ios - 停止特定的 UIView 动画 block - iOS

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:32 25 4
gpt4 key购买 nike

我有一个 iOS 应用程序,它运行几个不同的 UIViewAnimation block 来为屏幕上的几个不同对象设置动画。这一切都有效,但是我怎样才能停止其中一个动画 block 而不停止其余的呢?

我尝试过使用以下方法:

[button.layer removeAllAnimations];

但它什么也没做,动画只是继续。

然后我尝试使用一个简单的 BOOL 值,并在 BOOL 设置为“NO”后让动画从方法中返回 ",但这也不起作用。

这是我要停止的动画:

-(void)undo_animation:(int)num {

// Fade out/in the number button label
// animation which lets the user know
// they can undo this particular action.

[UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{

// Mostly fade out the number button label.
((UIButton *)_buttons[num]).alpha = 0.2;

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{

// Fade in the number button label.
((UIButton *)_buttons[num]).alpha = 1.0;

} completion:^(BOOL finished) {

// Stop the animation if the BOOL
// is set to 'NO' animations.

if (anim_state_button == NO) {
return;
}
}];
}];
}

谢谢,丹。

最佳答案

如果您使用的是 UIKit 动画,则无法访问正在运行的动画属性。所以如果你想在运行时修改动画,我建议使用 Core Animation。

像下面这样移除 View 的 alpha 太简单了。

CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"MyAnimation"];

将动画添加到图层后动画将开始,然后您可以使用委托(delegate)方法来通知 animationDidFinish: 方法

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
NSLog(@"Animation interrupted: %@", (!flag)?@"Yes" : @"No");
}

此外,您可以随时使用;

[[moviepic layer] animationForKey:@"MyAnimation"];

当然,您需要将 CoreAnimation Framework 添加到您的项目中。

希望对您有所帮助。

关于ios - 停止特定的 UIView 动画 block - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31284965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com