【发布时间】:2014-03-03 10:05:36
【问题描述】:
我正在编写一个 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。有人对这种奇怪的行为有解释吗?
我在做的细节是:
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];
}
}
【问题讨论】:
标签: ios objective-c uiviewcontroller quartz-core