iPhone 旋转后 UIScrollView 中的 contentOffset

发布于 2024-10-03 16:30:41 字数 1192 浏览 3 评论 0原文

我在 UIScrollView 中有一个 UIImageView,并且在使用 contentOffset 属性时遇到了一些问题。根据 Apple 的参考,其定义为:

contentOffset:内容视图的原点相对于滚动视图的原点的偏移点。

例如,如果图像位于屏幕的左上角,如下所示,则 contentOffset 将为 (0,0):

   _________
   |IMG    |
   |IMG    |
   |       |
   |       |
   |       |
   |       |
   ---------

对于设备旋转,我有以下设置:

 scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
       UIViewAutoresizingFlexibleHeight);

 imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
       UIViewAutoresizingFlexibleHeight);

 imageView.contentMode = UIViewContentModeCenter;  
    scrollView.contentMode = UIViewContentModeCenter;

这将使所有内容围绕屏幕中心旋转。 旋转屏幕后,屏幕将如下所示:

    ______________
    |       IMG  |
    |       IMG  |
    |            |
    --------------

我的问题是,如果我现在读取 contentOffset,它仍然是 (0,0)。 (如果我在横向模式下移动 UIImage,contentOffset 的值会更新,但它是根据错误的原点计算的。)

有没有办法计算 UIImage 的坐标屏幕的左上角。 contentOffset 似乎仅在屏幕处于视图的初始方向时返回此值。

我尝试读取 self.view.transform 和 scrollView.transform ,但它们始终是身份。

I have an UIImageView in an UIScrollView and I have some trouble with the contentOffset property. From Apple's reference, this is defined as:

contentOffset: The point at which the origin of the content view is offset from the origin of the scroll view.

For example, if the image is at the upper left corner of the screen as below then the contentOffset would (0,0):

   _________
   |IMG    |
   |IMG    |
   |       |
   |       |
   |       |
   |       |
   ---------

For device rotation I have the following setting:

 scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
       UIViewAutoresizingFlexibleHeight);

 imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
       UIViewAutoresizingFlexibleHeight);

 imageView.contentMode = UIViewContentModeCenter;  
    scrollView.contentMode = UIViewContentModeCenter;

This will make everything rotate around the center of the screen.
After rotating the screen, then screen will then look like this:

    ______________
    |       IMG  |
    |       IMG  |
    |            |
    --------------

My problem is that if I now read of contentOffset, it is still (0,0). (If I move the UIImage in landscape mode, the value of contentOffset is updated, but it is computed with respect to the wrong origin.)

Is there a way to compute the coordinates for the UIImage with respect to the upper left corner of the screen. contentOffset only seems to return this value when the screen is in the initial orientation of the view.

I have tried to read self.view.transform and scrollView.transform, but they are always the identity.

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

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

发布评论

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

评论(1

小姐丶请自重 2024-10-10 16:30:41

这是一种方法:对于滚动视图集,

scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth 
                                     | UIViewAutoresizingFlexibleHeight);

scrollView.contentMode = UIViewContentModeTopRight;

即使旋转行为不正确,UIViewContentModeTopRight 模式也会将左上角保持在坐标 (0,0) 处。要获得与 UIViewContentModeCenter 中相同的旋转行为,请添加

    scrollView.contentOffset = fix(sv.contentOffset, currentOrientation, goalOrientation);

willAnimateRotationToInterfaceOrientation 中。 fix 是函数

CGPoint fix(CGPoint offset, UIInterfaceOrientation currentOrientation, UIInterfaceOrientation goalOrientation) {

CGFloat xx = offset.x;
CGFloat yy = offset.y;

CGPoint result;

if (UIInterfaceOrientationIsLandscape(currentOrientation)) {

    if (UIInterfaceOrientationIsLandscape(goalOrientation)) {
        // landscape -> landscape
        result = CGPointMake(xx, yy);
    } else {
        // landscape -> portrait
        result = CGPointMake(xx+80, yy-80);
    }
} else {
    if (UIInterfaceOrientationIsLandscape(goalOrientation)) {
        // portrait -> landscape
        result = CGPointMake(xx-80, yy+80);
    } else {
        // portrait -> portrait
        result = CGPointMake(xx, yy);
    }
}
return result;
}

上面的代码将使滚动视图围绕屏幕中心旋转,并确保左上角坐标始终为(0,0)坐标。

Here is one way to do this: For the scrollview set

scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth 
                                     | UIViewAutoresizingFlexibleHeight);

scrollView.contentMode = UIViewContentModeTopRight;

The UIViewContentModeTopRight mode will keep the upper left corner at coordinate (0,0) even if the rotation behaviour is not correct. To get the same rotation behaviour as in UIViewContentModeCenter add

    scrollView.contentOffset = fix(sv.contentOffset, currentOrientation, goalOrientation);

into willAnimateRotationToInterfaceOrientation. fix is the function

CGPoint fix(CGPoint offset, UIInterfaceOrientation currentOrientation, UIInterfaceOrientation goalOrientation) {

CGFloat xx = offset.x;
CGFloat yy = offset.y;

CGPoint result;

if (UIInterfaceOrientationIsLandscape(currentOrientation)) {

    if (UIInterfaceOrientationIsLandscape(goalOrientation)) {
        // landscape -> landscape
        result = CGPointMake(xx, yy);
    } else {
        // landscape -> portrait
        result = CGPointMake(xx+80, yy-80);
    }
} else {
    if (UIInterfaceOrientationIsLandscape(goalOrientation)) {
        // portrait -> landscape
        result = CGPointMake(xx-80, yy+80);
    } else {
        // portrait -> portrait
        result = CGPointMake(xx, yy);
    }
}
return result;
}

The above code will make the scrollview rotate around the center of the screen and also ensure that the upper left corder is always are coordinates (0,0).

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