如何停止一个UIView的重复动画效果(UIViewAnimationOptionRepeat)?

3

我在一个UIView子类中有以下代码:

[element setAlpha: 0.0f];

[UIView animateWithDuration: 0.4 delay: 0.0 options: UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations: ^{
    [element setAlpha: 1.0f];
} completion: ^(BOOL finished){
    if (finished) 
    {
        return;
    }
}];

当我想停止动画时,我会向视图本身发送以下消息:

[[self layer] removeAllAnimations];

但它什么也没做... 我该如何停止动画?


不要忘记:#import <QuartzCore/QuartzCore.h> - Malloc
1
尝试使用 [self.layer removeAllAnimations];,另外,请问您是否收到任何编译警告? - Malloc
不,也许是我调用[self.layer removeAllAnimations]的地方有问题。我应该在视图内部调用它还是在包含视图的ViewController中调用? - Alex Tarragó
你应该在element.layer上调用 removeAllAnimations,而不是 self.layer,你认为呢? - neilco
1个回答

4

我使用以下代码进行了修复:

- (void)restoreAnimations {

    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear animations:^{
        [element setAlpha: 1.0f];
    } completion:NULL];
}

在ViewController中调用它。
顺便说一下,非常感谢Malloc!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接