2010-11-20 26 views
5

UIScrollViewUIImageViewがあり、contentOffsetプロパティに問題があります。 Appleのリファレンスから、iPhoneの回転後にUIScrollViewのcontentOffset

contentOffset:コンテンツビューの原点がスクロールビューの原点からオフセットされたポイント。例えば

、画像がcontentOffset次いで、以下のように画面の左上隅にある場合であろう(0,0):デバイスの回転のため

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

Iは次の設定を有する:

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が

scrollView.autoresizingMask =(UIViewAutoresizingFlexibleWidth 
            | UIViewAutoresizingFlexibleHeight); 

scrollView.contentMode = UIViewContentModeTopRight; 

を設定するためにUIViewContentModeTopRightモードは、回転動作が正しくない場合でも、(0,0)の座標左上隅を維持します。 UIViewContentModeCenterと同じ回転動作を得るには、

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

willAnimateRotationToInterfaceOrientationに追加します。 fixはscrollviewを行います上記のコードは、画面の中心の周りを回転し、また、左上コーダーは常に、座標(0,0)であることを確認機能

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

あります。

関連する問題