2010-11-20 82 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)。