我正在寫一個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];
}
}