动画过渡在横向方向上无法正常工作

发布于 2024-10-12 17:21:03 字数 702 浏览 2 评论 0原文

我的应用程序仅支持横向右方向。

我正在尝试转换到视图控制器的视图,但是当它执行过渡动画(例如卷曲)时,视图旋转 90 度(因此,模拟器仍处于横向右方向,但视图显示为旋转) 90 度)。过渡完成后,它会旋转到正确的方向。我的代码如下。

- (IBAction)buttonTouched
{
    MyViewController *aViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];

    [UIView transitionFromView:self.view 
                        toView:aViewController.view 
                      duration:2 
                       options:UIViewAnimationOptionTransitionCurlUp 
                    completion:NULL];
}

视图似乎没有收到“方向已更改”通知,因此它以纵向显示,然后在动画完成后更改为横向。我已经在 IB 中将其设置为横向布局。

编辑:我还尝试将视图控制器添加到自身的 IB 文件并转换到该视图(认为初始化和转换之间可能太接近),但发生了同样的事情。

My app only supports the landscape right orientation.

I'm trying to transition into a view controller's view, but when it performs the transition animation (such as curl up), the view is rotated 90 degrees (so, the simulator is still in landscape-right orientation, but the view appears rotated by 90 degrees). When the transition finishes it rotates to the correct orientation. My code is below.

- (IBAction)buttonTouched
{
    MyViewController *aViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];

    [UIView transitionFromView:self.view 
                        toView:aViewController.view 
                      duration:2 
                       options:UIViewAnimationOptionTransitionCurlUp 
                    completion:NULL];
}

It appears as though the view doesn't get the "orientation changed" notification, so it presents it in portrait, and then changes to landscape after the animation finishes. I have set it up in IB so that it is all laid out in landscape.

EDIT: I also tried adding a View Controller to self's IB file and transitioning to that view (thinking maybe it was too close between the initialization and the transition) but the same thing happened.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-10-19 17:21:03

好吧,主要原因是 self.view (当前“知道”您处于横向状态)正在被一个没有该信息的新视图替换。所以一种想法是将 aViewController.view 作为 self.view 的子视图(假设 aViewController 是不透明的)。啊,你说,但是我失去了transitionFromView 的漂亮动画。好吧,尝试一下这个好处:

[UIView transitionWithView:self.view
                  duration:2 
                   options:UIViewAnimationOptionTransitionCurlUp
                animations:^{
                    [self.view addSubview:aViewController.view];
                } 
                completion:NULL];

如果由于某种原因这对你不起作用,另一种方法是“教导”aViewController.view它实际上是一个横向视图:

#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
-(void)setNewViewToLandscape:(UIView*)viewObject {
     //assumes self.view is landscape and viewObject is portrait
     [viewObject setCenter:CGPointMake( self.view.frame.size.width/2,self.view.frame.size.height/2)];
     CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
     viewObject.transform = cgCTM;
     viewObject.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}

 MyViewController *aViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];
[self setNewViewToLandscape:aViewController.view];

[UIView transitionFromView:self.view 
                    toView:aViewController.view 
                  duration:2 
                   options:UIViewAnimationOptionTransitionCurlUp 
                completion:NULL];

还有一点是你的aViewController正在泄漏,所以你应该它是父视图控制器的保留属性。

Well, the main reason is that self.view (which currently "knows" that you're in landscape) is being replaced with a new view which doesn't have that information. So one thought is to just put aViewController.view as a subView of self.view (assuming that aViewController is opaque). Ah, you say, but then I lose the nice animation of transitionFromView. Well, try this niceness:

[UIView transitionWithView:self.view
                  duration:2 
                   options:UIViewAnimationOptionTransitionCurlUp
                animations:^{
                    [self.view addSubview:aViewController.view];
                } 
                completion:NULL];

If for some reason that doesn't work for you, the alternative is to "teach" aViewController.view that it's really a landscape view:

#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
-(void)setNewViewToLandscape:(UIView*)viewObject {
     //assumes self.view is landscape and viewObject is portrait
     [viewObject setCenter:CGPointMake( self.view.frame.size.width/2,self.view.frame.size.height/2)];
     CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));
     viewObject.transform = cgCTM;
     viewObject.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}

 MyViewController *aViewController = [[MyViewController alloc] initWithNibName:NSStringFromClass([MyViewController class]) bundle:nil];
[self setNewViewToLandscape:aViewController.view];

[UIView transitionFromView:self.view 
                    toView:aViewController.view 
                  duration:2 
                   options:UIViewAnimationOptionTransitionCurlUp 
                completion:NULL];

One minor additional point is that your aViewController is leaking, so you should make it a retained property of the parent viewController.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文