画像ビューをドラッグして回転させる機能を備えたアプリを開発しています。
以下のコードを使用して、ドラッグと回転を正常に実装しました
In touchesBegan メソッド
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
touchStart = [[touches anyObject] locationInView:imageView];
isResizingLR = (containerVw.bounds.size.width - touchStart.x < kResizeThumbSize && containerVw.bounds.size.height - touchStart.y < kResizeThumbSize);
}
In touchesMoved メソッド
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:containerVw];
CGPoint previous=[[touches anyObject]previousLocationInView:containerVw];
UITouch *touch = [[event allTouches] anyObject];
if (isResizingLR) {
CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:containerVw.superview]] ;
CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:containerVw.superview]] ;
// the current value of the rotation angle
CGFloat tranA = atan2(containerVw.transform.b, containerVw.transform.a) ;
// the angle difference between last touch and the current touch
CGFloat diffA = currA - prevA ;
// the new angle resulting from applying the difference
CGFloat angle1 = tranA - diffA ;
CGAffineTransform t = CGAffineTransformMakeRotation(angle1) ;
containerVw.transform =t;
}
if (!isResizingLR) {
containerVw.center = CGPointMake(containerVw.center.x + touchPoint.x - touchStart.x, containerVw.center.y + touchPoint.y - touchStart.y);
}
}
私の問題
上記のコードは、ドラッグ後に画像ビューを回転するまで正常に機能しています。画像ビューを回転させた後にドラッグした場合。画像ビューが画面から離れました。
touchesEnd メソッドで、画像ビュー フレームを印刷しようとしました。たとえば、最初の画像ビュー フレームは (100,100,150,149) です。回転後、画像ビュー 160° フレームは (103,31,182,183) に変更され、現在は正常に動作しています。
画像ビューを移動しようとすると回転した後、画像ビューが画面から離れ、フレームが (615,-212,182,183) に変更されました
解決方法を教えてください。