2015-12-10 107 views
0

我已經創建了一個UIView動畫使用下面的代碼,通常是它工作得很好,但有時它會與崩潰:的iOS setAnimationDidStopSelector方法導致崩潰

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]; 
} 
+0

_「在iOS 4及更高版本中,使用基於塊的動畫方法。 ://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/),以及...... iOS4於5年前發佈。 – holex

回答

0

EXC_BAD_ACCESS意味着某事被釋放,但你仍然有指向該內存的指針,並且當你試圖發送消息給這個無效指針時,它會崩潰。

  • (無效)setAnimationDelegate:(ID _Nullable)代表說明

設置任意的動畫消息的委託。一個對象,用於定義使用setAnimationWillStartSelector註冊的方法 :和setAnimationDidStopSelector:方法。該視圖在動畫期間保持對該對象的強引用 。

在你的情況下,視圖會有很強的自我引用,這很糟糕。 看起來像應用程序崩潰,因爲有時動畫發佈引用animationDelegate之前它被設置爲setAnimationDelegate因爲一些滯後或什麼。 *嘗試避免這種動畫方式,使用塊 *嘗試傳遞__weak typeof(self)wSelf = self;任何代表和內部塊 *看起來你有沒有辦法阻止該動畫,也應用泄漏

,使圖像的無窮遠旋轉,你可以簡單地做:

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"]; 

只要記住你在從後臺或呈現視圖返回時重新啓動它,例如在彈出到視圖控制器之後重新啓動它。