gpt4 book ai didi

ios - UIViewController didRotateFromInterfaceOrientation : not being called

转载 作者:行者123 更新时间:2023-11-28 20:07:30 24 4
gpt4 key购买 nike

我正在编写一个 iOS 游戏应用程序,其中有一些 View 在屏幕上飞来飞去,应该可以在横向和纵向模式下播放。对于旧设备,出于性能原因,我决定在界面旋转时停止动画。应按照苹果文档中的描述停止和继续动画 https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coreanimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html效果很好。所以我决定在 willRotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation duration: (NSTimeInterval) duration 中暂停动画,并在 didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation 中恢复动画。问题是,如果我将 View 的图层速度设置为零,则永远不会调用 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];
}
}

最佳答案

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

来自 UIViewController 类引用:

Your implementation of this method must call super at some point during its execution.

这是您的实现的更正版本:

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

希望这能解决您的问题。

关于ios - UIViewController didRotateFromInterfaceOrientation : not being called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21603280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com