首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIViewController didRotateFromInterfaceOrientation:没有被调用

UIViewController didRotateFromInterfaceOrientation:没有被调用
EN

Stack Overflow用户
提问于 2014-02-06 12:40:17
回答 1查看 3.5K关注 0票数 0

我正在编写一个iOS游戏应用程序,一些视图在屏幕周围飞行,这应该是可玩的景观和肖像模式。关于较旧的设备,由于性能原因,我决定在界面旋转过程中停止动画。动画应该停止并继续,如apples guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html中所描述的,它工作得很好。所以我决定暂停willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration中的动画,然后在didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation中继续它。问题是,如果我将视图的层速度设置为零,则永远不会调用didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation。有人对这种奇怪的行为有什么解释吗?

我要做的是:

代码语言:javascript
复制
SGMathsMasterNoteView *noteView = [SGMathsMasterNoteView noteViewWithExercise: self.nextExercise andDelegate: self];
[self.animationContainer insertSubview: noteView aboveSubview: self.noteStackView];
[noteView startAnimation];

SGMathsMasterNoteView.m

代码语言:javascript
复制
- (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

代码语言:javascript
复制
- (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];
   }
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-22 15:17:23

乍一看,您的willRotateToInterfaceOrientation:duration:didRotateFromInterfaceOrientation:方法不调用所需的super。我的猜测是,由于super不是在旋转之前被调用的,所以它阻止了调用旋转后的方法。

来自UIViewController类引用:

在此方法的执行过程中,您的实现必须在某个时候调用超级。

下面是您的实现的更正版本:

代码语言:javascript
复制
- (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打了电话,最后尝试调用它。

希望这能解决你的问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21603280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档