简体   繁体   English

iOS setAnimationDidStopSelector方法导致崩溃

[英]iOS setAnimationDidStopSelector method causes crash

I have created a UIView animation use the below code, usually it works fine, but occasionally it will crash with: 我使用以下代码创建了一个UIView动画,通常可以正常使用,但偶尔会崩溃:

EXC_BAD_ACCESS(code=2,address=0xcc) EXC_BAD_ACCESS(代码= 2,地址= 0xcc)

 -(void)continueAnimation
{   
    [button setImage:[UIImage imageNamed:REFRESHING_IMAGE] forState:UIControlStateNormal];
    if (leastTime <= 0) {
        leastTime += 360;
    }   
    leastTime -= 10;
    angle += 10;
    angle %= 360;
    [button setEnabled:NO];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.01];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(continueAnimation)];
    button.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
    [UIView commitAnimations];
}

EXC_BAD_ACCESS means that something was released, but you still have pointer to that memory, and when you try to send message to this invalid pointer it crashes. EXC_BAD_ACCESS表示已释放某些内容,但是您仍然具有指向该内存的指针,并且当您尝试向该无效的指针发送消息时会崩溃。

  • (void)setAnimationDelegate:(id _Nullable)delegate Description (void)setAnimationDelegate:{id _Nullable)delegate说明

Sets the delegate for any animation messages. 设置任何动画消息的委托。 An object that defines the methods registered using the setAnimationWillStartSelector: and setAnimationDidStopSelector: methods. 一个对象,该对象定义使用setAnimationWillStartSelector:和setAnimationDidStopSelector:方法注册的方法。 The view maintains a strong reference to this object for the duration of the animation. 在动画过程中,该视图对该对象保持强烈的引用。

In you case view will have strong ref to itself and that's bad. 在您的情况下,视图将具有强大的参照力,这很不好。 Looks like application crashes because sometimes animation releases ref to animationDelegate before it get setted to setAnimationDelegate because of some lag or something. 看起来应用程序崩溃,是因为有时由于某些滞后或某些原因,动画在将其设置为setAnimationDelegate之前会先释放对animationDelegate的引用。

Anyway * try to avoid such a way of animation, use blocks * try to pass __weak typeof(self) wSelf = self; 无论如何*尝试避免这种动画方式,使用块*尝试传递__weak typeof(self)wSelf = self; to any delegates and inside blocks * looks like you have no way to stop that animation and also application leaks 到任何代表和内部块*看起来您无法停止该动画以及应用程序泄漏

to make infinit rotation of image you can simply do: 要无限旋转图像,您可以执行以下操作:

    CABasicAnimation* rotationAnimation;

    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = HUGE_VALF;
    [self.spinnerImg.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

just keep in mind that you have restart it on returning from background or presenting view, for example after pop to viewcontroller 只需记住您已从后台返回或显示视图时重新启动它,例如在弹出视图控制器之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM