我正在开发一个应用程序,它具有通过拖动右下角按钮来调整图像视图大小和旋转的功能。
我看到一个应用程序具有以下功能:如果我们沿对角线拖动右下角按钮,图像视图的大小就会调整大小,或者如果我们向左或向右拖动按钮,图像视图就会按方向旋转。我希望在我的应用程序中实现此功能
我正在努力实现单指旋转以及调整图像视图的大小。
我可以通过拖动它的右下角按钮成功地实现调整图像视图的大小。但是我没有足够的知识来为图像视图添加旋转
请以正确的方式指导我。
我添加了下面的代码,通过拖动它的右角来调整图像视图的大小。
- (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);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:imageView];
CGPoint previous=[[touches anyObject]previousLocationInView:imageView];
UITouch *touch = [[event allTouches] anyObject];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
if (isResizingLR) {
containerVw.frame = CGRectMake(containerVw.frame.origin.x, containerVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
dragIm.frame = CGRectMake(containerVw.frame.size.width-10, containerVw.frame.size.height-10,20,20);
if (!isResizingLR) {
containerVw.center = CGPointMake(containerVw.center.x + touchPoint.x touchStart.x,containerVw.center.y + touchPoint.y - touchStart.y);
}
}
我遇到了和你一样的障碍,所以我开发了自己的模块ZDStickerView。这将是很好的参考。
首先,确保您的视图的 autoresizingMask 应该是灵活的。
autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
否则调整大小将无法正常工作。
其次,我推荐你使用“CGAffineTransformMakeRotation”和“atan2”函数来解决旋转问题,像这样:
float ang = atan2([recognizer locationInView:self.superview].y - self.center.y,
[recognizer locationInView:self.superview].x - self.center.x);
float angleDiff = deltaAngle - ang;
self.transform = CGAffineTransformMakeRotation(-angleDiff);
第三,一定要使用相对坐标,像这样:
self.transform = CGAffineTransformMakeRotation(-angleDiff);
我已经修复了这个问题并在 cocoacontrols 中上传了这个控件
http://www.cocoacontrols.com/controls/tdresizerview
和示例代码
https://www.dropbox.com/s/yb0vzy0wao52bgt/SampeResizeView.zip
更新的答案
这可能有帮助,https://github.com/zedoul/ZDStickerView.
具有 OneFingerRotation、缩放、调整大小和关闭功能的 IQStickerView。
特点: