【问题标题】:contentOffset in UIScrollView after rotation of iPhoneiPhone旋转后UIScrollView中的contentOffset
【发布时间】:2010-11-20 12:55:47
【问题描述】:

我在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.transformscrollView.transform,但它们始终是身份。

【问题讨论】:

    标签: uiscrollview uiimageview


    【解决方案1】:

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

    scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth 
                                         | UIViewAutoresizingFlexibleHeight);
    
    scrollView.contentMode = UIViewContentModeTopRight;
    

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

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

    进入willAnimateRotationToInterfaceOrientationfix 是函数

    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;
    }
    

    上面的代码会让scrollview围绕屏幕中心旋转,同时保证左上角的坐标始终是坐标(0,0)。

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多