2010-11-20 60 views
5

我有一个UIImageViewUIScrollView和我有一些contentOffset属性的麻烦。根据Apple的参考文献,其定义如下:contentOffset在UIScrollView旋转后的iPhone

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,但他们总是身份。

回答

3

下面是做到这一点的一种方法:对于滚动视图设置

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

上面的代码将滚动视图绕屏幕的中心,也是确保左上角科德始终是坐标为(0,0)。