2011-01-13 8 views
1

私はUIImageViewにボタンを追加しました。ユーザーが画面に触れると、UIImageViewが回転するので、回転が行われた後にボタンの新しい位置を取得する方法があるかどうかを知りたいと思います。UIViewを回転させた後にUIButtonの位置を確認します

は、今のところ私はこの方法では元の位置にすべての時間を取得しています:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    NSLog(@"Xposition : %f", myButton.frame.origin.x); 
    NSLog(@"Yposition : %f", myButton.frame.origin.y); 

} 

おかげで、

答えて

6

これはトリッキーな質問です。

警告:transformプロパティがID変換でない場合、このプロパティの値は定義されていないため、無視する必要があります。

だからといって、解決策が見つかっており、正確に何が必要なのかによって異なります。近似が必要な場合、または回転角度が常に90度の場合は、CGRectApplyAffineTransform()関数が十分に機能するかもしれません。ボタンの現在の変換とともにUIButtonの(変換されていない)フレームを渡すと、変換されたrectが得られます。 rectは原点、幅、高さとして定義されているため、画面の端と平行でない辺を持つ矩形を定義することはできません。平行でない場合は、回転した矩形に対して可能な限り小さな境界矩形を返します。任意のマイナーを許す

- (void)computeCornersOfTransformedView:(UIView*)transformedView relativeToView:(UIView*)parentView { 
    /* Computes the coordinates of each corner of transformedView in the coordinate system 
    * of parentView. Each is corner represented by an independent CGPoint. Doesn't do anything 
    * with the transformed points because this is, after all, just an example. 
    */ 

    // Cache the current transform, and restore the view to a normal position and size. 
    CGAffineTransform cachedTransform = transformedView.transform; 
    transformedView.transform = CGAffineTransformIdentity; 

    // Note each of the (untransformed) points of interest. 
    CGPoint topLeft = CGPointMake(0, 0); 
    CGPoint bottomLeft = CGPointMake(0, transformedView.frame.size.height); 
    CGPoint bottomRight = CGPointMake(transformedView.frame.size.width, transformedView.frame.size.height); 
    CGPoint topRight = CGPointMake(transformedView.frame.size.width, 0); 

    // Re-apply the transform. 
    transformedView.transform = cachedTransform; 

    // Use handy built-in UIView methods to convert the points. 
    topLeft = [transformedView convertPoint:topLeft toView:parentView]; 
    bottomLeft = [transformedView convertPoint:bottomLeft toView:parentView]; 
    bottomRight = [transformedView convertPoint:bottomRight toView:parentView]; 
    topRight = [transformedView convertPoint:topRight toView:parentView]; 

    // Do something with the newly acquired points. 
} 

してください:あなたは1または形質転換の点の全ての正確な座標を知る必要がある場合さて、私は前にそれらを計算するためのコードを書いたが、それは少しより複雑だ

コードのエラー、私はブラウザでそれを書いた。最も有用なIDEではありません...

+3

あなたはそのようなコードを書くことができますが、それは驚くべきことです。ウワ!私はそれを綴る方法を忘れてしまい、別のプロジェクトからコピー&ペーストする必要があるので、私は "無効"を過ぎていません。 – Fattie

+0

'transformedView.transform = CGAffineTransformIdentity;に対しては+1です。 – rptwsthi

関連する問題