我正在开发一个应用程序,它具有拖动和旋转图像视图的功能。
我已经使用下面的代码实现了成功拖动和旋转
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);
}
在 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)
请指导我如何解决它。