How to rotate and resize the image view with single finger(如何用单指旋转和调整图像视图的大小)
问题描述
我正在开发一个应用程序,它具有通过拖动其右下角按钮来调整和旋转图像视图的功能.
I am developing an app which has feature that resizing and rotating the imageview by dragging its bottom right corner button.
我看到一个应用程序具有这样的功能:如果我们将右下角按钮对角拖动,则图像视图大小已调整大小,或者如果我们将按钮向左或右侧方向拖动,则图像视图已按方向旋转.我希望在我的应用中实现此功能
I saw one app which has feature that if we drag the bottom right corner button diagonally imageview size had resized or else if we drag the button left or right side direction imageview had rotated as per direction. I wish to implement this feature in my app
我正在努力实现单指旋转以及调整图像视图的大小.
I am struggling to implement single finger rotation as well as resizing the imageview.
我可以通过拖动其右下角按钮成功实现调整图像视图的大小.但是我没有足够的知识来为图像视图添加旋转
I could successfully implement resizing the imageview by dragging its bottom right corner button. But I do not have enough knowledge to add rotation to the image view
请以正确的方式指导我.
Please guide me in right way.
我添加了以下代码,用于通过拖动右上角来调整图像视图的大小.
I have added the code below for resizing the image view by dragging its right corner.
- (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.这将是很好的参考.
I had hitted the same obstacle as yours, so I developed my own modules ZDStickerView. It would be nice reference.
首先,确保您的视图的 autoresizingMask 应该是灵活的.
First of all, be sure your view's autoresizingMask should be flexible.
autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
否则调整大小将无法正常工作.
otherwise resizing wouldn't work properly.
其次,我推荐你使用CGAffineTransformMakeRotation"和atan2"函数来解决旋转问题,像这样:
Second, I recommends you to use "CGAffineTransformMakeRotation" and "atan2" functions to solve the rotation problem, like this:
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);
第三,一定要用相对坐标,像这样:
Third, be sure to use relative coordinate, like this:
self.transform = CGAffineTransformMakeRotation(-angleDiff);
这篇关于如何用单指旋转和调整图像视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何用单指旋转和调整图像视图的大小
基础教程推荐
- 更改 UITableView 部分标题的颜色 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- iOS4 创建后台定时器 2022-01-01