2014-02-06 64 views
0

我正在寫一個iOS遊戲應用程序,有一些視圖在屏幕上飛行,應該可以在橫向和縱向模式下播放。對於舊設備和出於性能方面的原因,我決定在界面旋轉過程中停止動畫。動畫應該停止並繼續,正如蘋果文檔https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coreanimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html中描述的那樣,它工作正常。所以我決定在willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration暫停動畫,並在didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation恢復。問題是,如果我將視圖的圖層速度設置爲零,則從不會調用didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation。有沒有人對這種奇怪的行爲有解釋?UIViewController didRotateFromInterfaceOrientation:不被稱爲

我在做什麼具體是:

SGMathsMasterNoteView *noteView = [SGMathsMasterNoteView noteViewWithExercise: self.nextExercise andDelegate: self]; 
[self.animationContainer insertSubview: noteView aboveSubview: self.noteStackView]; 
[noteView startAnimation]; 

SGMathsMasterNoteView.m

- (void) startAnimation { 
    self.frame = self.noteAnimationView.noteStackView.frame; 
    CAAnimation *ani = self.noteAnimationView.motionAnimation; 
    [self.layer addAnimation: ani forKey: @"fly"]; 
    self.layer.speed = 1.0; 
    self.center = self.noteAnimationView.destroyPoint; 
} 

- (void) pauseAnimation { 
    self.layer.speed = 0.0; 
    self.layer.timeOffset = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
} 

- (void) resumeAnimation { 
    CFTimeInterval pausedTime = self.layer.timeOffset; 
    self.layer.speed = 1.0; 
    self.layer.timeOffset = 0.0; 
    self.layer.beginTime = 0.0; 
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; 
    self.layer.beginTime = timeSincePause; 
} 

SGGameViewController.m

- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration { 
    for (SGMathsMasterNoteView *noteView in [self.animationContainer.subviews allObjectsOfClass: [SGMathsMasterNoteView class]]) { 
     [noteView pauseAnimation]; 
    } 
} 

- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation { 
    for (SGMathsMasterNoteView *noteView in [self.animationContainer.subviews allObjectsOfClass: [SGMathsMasterNoteView class]]) { 
     [noteView resumeAnimation]; 
    } 
} 

回答

3

乍一看你willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation:方法唐不要求super這是必需的。我的猜測是,因爲super在旋轉之前未被調用,所以它會阻止調用後旋轉方法。

UIViewController類參考:

此方法的實現必須在其執行過程中的一些點 超級調用。

這是你實現的修正版本:

- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration 
{ 
    [super willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration]; 

    for (SGMathsMasterNoteView *noteView in [self.animationContainer.subviews allObjectsOfClass: [SGMathsMasterNoteView class]]) 
    { 
     [noteView pauseAnimation]; 
    } 
} 

- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation 
{ 
    [super didRotateFromInterfaceOrientation: fromInterfaceOrientation]; 

    for (SGMathsMasterNoteView *noteView in [self.animationContainer.subviews allObjectsOfClass: [SGMathsMasterNoteView class]]) 
    { 
     [noteView resumeAnimation]; 
    } 
} 

我叫super但是首先嚐試在最後調用它。

希望能夠解決您的問題。