UIViewController didRotateFromInterfaceOrientation:没有被调用

时间:2014-02-06 12:40:17

标签: ios objective-c uiviewcontroller quartz-core

我正在编写一个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];
   }
}

1 个答案:

答案 0 :(得分:3)

乍一看,willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation:方法不会调用所需的super。我的猜测是,因为在旋转之前没有调用super,所以它会阻止调用后旋转方法。

来自UIViewController班级参考:

  

此方法的实现必须在某个时候调用super   在执行期间。

以下是您的实施的更正版本:

- (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然后尝试在最后调用它。

希望能解决您的问题。