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