我正在开发一个应用程序,它具有通过拖动右下角按钮来调整 ImageView 大小和旋转 ImageView 的功能。
我看到一个应用程序的功能是,如果我们沿对角线拖动右下角的按钮, ImageView 的大小就会调整,或者如果我们向左或向右拖动按钮, ImageView 就会按方向旋转。我希望在我的应用中实现此功能
我正在努力实现单指旋转以及调整 imageview 的大小。
我可以通过拖动其右下角按钮成功实现调整 ImageView 的大小。但是我没有足够的知识来为 ImageView 添加旋转
请以正确的方式指导我。
我添加了下面的代码,通过拖动它的右角来调整 ImageView 的大小。
- (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 .这将是很好的引用。
首先,确保您的 View 的 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);
关于iphone - 如何用单指旋转和调整 ImageView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12566791/