在 UIView 上旋转/移动对象

发布于 2024-09-29 17:57:39 字数 1257 浏览 4 评论 0原文

当方向改变时,我需要旋转和移动 UIView 上的对象。为此,我在 willRotateToInterfaceOrientation 中添加了以下内容,

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
    [self repositionObjectAfterRotation:scrollView x:0 y:100 width:480 height:150];
    [self repositionObjectAfterRotation:pageControl x:50 y:430 width:100 height:30];    
}

我的 repositionObjectAfterRotation:x:y:width:height: 看起来像这样,它在设置新的界限。

- (void)repositionObjectAfterRotation:(UIView *)anObject x:(int)x y:(int)y 
                                     width:(int)width height:(int)height
{   

    [UIView animateWithDuration:kAnimationDuration animations:^{
        CGRect boundsRect = anObject.bounds;
        boundsRect.origin.x = x;
        boundsRect.origin.y = y;
        boundsRect.size.width = width;
        boundsRect.size.height = height;
        anObject.bounds = boundsRect;
    } completion:^ (BOOL finished){
        if (finished) {
            [UIView animateWithDuration:kAnimationDuration animations:^{
                anObject.alpha = 1.0;
            }];
        }
    }];

}

当视图旋转回纵向时,它只显示滚动视图的一半,即使我的 NSLogs 声明框架和视图都显示为滚动视图。边界是正确的...

我是否正确旋转/重新定位对象,或者是否有更简单/首选的方式来支持景观?

谢谢

I need to rotate and move objects on a UIView when the orientation changes. To that end I have the following in willRotateToInterfaceOrientation

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
    [self repositionObjectAfterRotation:scrollView x:0 y:100 width:480 height:150];
    [self repositionObjectAfterRotation:pageControl x:50 y:430 width:100 height:30];    
}

My repositionObjectAfterRotation:x:y:width:height: looks like this, which takes the object and alters its bounds, before setting the new bounds.

- (void)repositionObjectAfterRotation:(UIView *)anObject x:(int)x y:(int)y 
                                     width:(int)width height:(int)height
{   

    [UIView animateWithDuration:kAnimationDuration animations:^{
        CGRect boundsRect = anObject.bounds;
        boundsRect.origin.x = x;
        boundsRect.origin.y = y;
        boundsRect.size.width = width;
        boundsRect.size.height = height;
        anObject.bounds = boundsRect;
    } completion:^ (BOOL finished){
        if (finished) {
            [UIView animateWithDuration:kAnimationDuration animations:^{
                anObject.alpha = 1.0;
            }];
        }
    }];

}

When the view rotates back to portrait, it only displays half of the scrollview, even though my NSLogs state that the frame & bounds are correct...

Am I rotating / repositioning objects correctly or is there an easier/preferred way to support landscape?

Thanks

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

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

发布评论

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

评论(2

夢归不見 2024-10-06 17:57:39

当您旋转到横向时,您明确将高度设置为“150”,但当您返回纵向时,不要将其调整回来。

我不知道你的自动调整大小是如何在 IB 中设置的,所以我无法说明这些对象在单独放置时将如何调整大小/移动。

You are explicitly setting the height to "150" when you rotate to Landscape, but don't adjust it back when you go back to portrait.

I don't know how your auto-sizing is set-up in IB, so I can't state how these objects will resize/move when left by themselves.

雨后彩虹 2024-10-06 17:57:39

3点:

  1. 你不需要显式地开始动画,系统无论如何都会这样做(除非你的动画“与旋转不同”。
  2. 我认为重写 layoutSubviews 更方便。它在每次旋转后调用,并且[设备方向]为您提供新的方向。
  3. 您可能想要更改这些视图的框架,而不是它们的边界。内部值(即仅在该视图“内部”起作用)

表示“将我置于 superivew 的这个位置”,这就是您想要的定义。例如,scrollView 用于其子视图的坐标是

layoutSubviews 中,只需更改框架,您想要的动画就会自动应用:

- (void)layoutSubviews {
    if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
        anObject.frame = landscapeFrame;
    }
    else {
        anObject.frame = portraitFrame;
    }
}

3 points:

  1. you don't need to explicitly begin an animation, system will do it anyway (unless your animation is "different from rotation."
  2. I think overriding layoutSubviews is more convenient. It's called after each rotation, and [device orientation] gives you the new orientation.
  3. You probably want to change the frame of these views, not their bounds, which is an internal value (i.e. it matters only "inside" that view).

frame says 'put me in this position of the superivew', which is what you want. bound defines the coordinate your, say, scrollView uses for its subviews. what you need here is the frame.

In layoutSubviews, just change the frame, the animation you want will apply automatically:

- (void)layoutSubviews {
    if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
        anObject.frame = landscapeFrame;
    }
    else {
        anObject.frame = portraitFrame;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文