【问题标题】:iOS: Animating Incorrect Orientation at rootViewController?iOS:在 rootViewController 动画不正确的方向?
【发布时间】:2012-04-08 05:37:28
【问题描述】:

上下文:我正在尝试执行从普通 UIViewController.viewUISplitViewController.view 的自定义动画。动画应该从左到右显示。

我设置了self.window.rootViewController = viewController,其中viewController 是一个普通的UIViewController

一旦用户滑动,就会调用以下代码:

UIView *theWindow = [viewController.view superview];

[viewController.view removeFromSuperview];
[theWindow addSubview:self.splitViewController.view];


CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];

当设备处于纵向模式时,一切都很完美。但是,当设备处于横向模式时,过渡动画就像设备仍处于纵向模式一样执行。例如:不是从左边进来,而是从底部进来。两个视图的方向都是完全正确的。只有过渡很奇怪。

【问题讨论】:

  • 如果向另一个方向旋转到横向,动画是否从顶部开始?还是无论您以哪种方式进入横向模式,它都是从底部开始的?该常量将动画从图层的左侧带入。不知道是不是图层的frame没有更新,所以图层的左边是旋转后的底部。
  • 没错。动画忽略任何方向变化。关于如何确保图层得到更新的任何建议?
  • 我有同样的问题 - CALayer 的方向似乎与它的视图不匹配。似乎还有其他一些类似的帖子,但似乎没有一个解决方案对我有用。将图层旋转 90 度当然也会旋转视图,所以这无济于事..

标签: ios xcode ipad animation orientation


【解决方案1】:

所以,我一直在玩弄您的代码(我对您的项目设置进行了最佳猜测重建),并且能够使用以下代码获得所需的效果。我尝试设置图层、视图或子图层等的框架和边界,但这些都不起作用。 CATransform3D、CATransform3DRotates 等也没有成功。我还用谷歌搜索了一个小时。要么没有人遇到你的问题,要么没有人解决你的问题。无论如何,这个解决方案是有效的,除非某些 Core Animation 大师可以提供更好的解决方案,否则欢迎您这样做:

- (void)swipeGesture:(UISwipeGestureRecognizer *)sender {

UIView *theWindow = [self.viewController.view superview];

UIInterfaceOrientation interfaceOrientation = self.viewController.interfaceOrientation;
NSString *subtypeDirection;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
    subtypeDirection = kCATransitionFromTop;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    subtypeDirection = kCATransitionFromBottom;
}
else {
    subtypeDirection = kCATransitionFromLeft;
}

[self.viewController.view removeFromSuperview];
[theWindow addSubview:self.splitViewController.view];

CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:subtypeDirection];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
}

【讨论】:

  • 此解决方案有效,但我希望它不会出现这种情况(手动调整过渡)。我有其他动画取决于不适用于此的窗口方向。除非出现更好的解决方案,否则我会在赏金结束之前将您的答案标记为正确。谢谢。
  • 明白。很抱歉我无法为您提供更好的解决方案。我建立了一个示例项目,并在昨天花了 3 个小时试图找到一些东西。这个想法很简单:更新窗口层的框架,使其与初始视图层的方向相匹配。可悲的是,将其付诸实践似乎并不那么简单。
  • 我认为这只会为过渡选择正确的方向吗?它仍然没有调整过渡的“框架”大小?
猜你喜欢
  • 1970-01-01
  • 2012-06-28
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多