【问题标题】:UIViewController didRotateFromInterfaceOrientation: not being calledUIViewController didRotateFromInterfaceOrientation:没有被调用
【发布时间】: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


    【解决方案1】:

    乍一看,您的 willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation: 方法不会调用 super,这是必需的。我的猜测是,由于 super 在旋转之前没有被调用,它会阻止调用 post-rotation 方法。

    来自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,然后试着在最后打电话。

    希望能解决您的问题。

    【讨论】:

      猜你喜欢
      • 2010-10-29
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多