我正在编写一个iOS游戏应用程序,一些视图在屏幕周围飞行,这应该是可玩的景观和肖像模式。关于较旧的设备,由于性能原因,我决定在界面旋转过程中停止动画。动画应该停止并继续,如apples 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];
}
}发布于 2014-03-22 15:17:23
乍一看,您的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打了电话,最后尝试调用它。
希望这能解决你的问题。
https://stackoverflow.com/questions/21603280
复制相似问题