UIImageView手势(缩放、旋转)问题
我想对 UIImageView
进行 2 次操作缩放、旋转,我有 2 个问题:
A. 我对前进行了缩放操作。当我尝试旋转时,
UIImageView
设置为初始大小,我想知道如何保持缩放的UIImageView
并从缩放图像进行旋转。< /p>B.我想将缩放操作与旋转结合起来,但我不知道如何实现:
- (void)viewDidLoad
{
foo = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 600, 800.0)];
foo.userInteractionEnabled = YES;
foo.multipleTouchEnabled = YES;
foo.image = [UIImage imageNamed:@"earth.jpg"];
foo.contentMode = UIViewContentModeScaleAspectFit;
foo.clipsToBounds = YES;
[self.view addSubview:foo];
}
//---pinch gesture---
UIPinchGestureRecognizer *pinchGesture =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
[foo addGestureRecognizer:pinchGesture];
[pinchGesture release];
//---rotate gesture---
UIRotationGestureRecognizer *rotateGesture =
[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotateGesture:)];
[foo addGestureRecognizer:rotateGesture];
[rotateGesture release];
//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender {
NSLog(@"Pinch");
CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
if (factor > 1) {
//---zooming in---
sender.view.transform = CGAffineTransformMakeScale(
lastScaleFactor + (factor-1),
lastScaleFactor + (factor-1));
}
else {
//---zooming out---
sender.view.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor);
}
if (sender.state == UIGestureRecognizerStateEnded) {
if (factor > 1) {
lastScaleFactor += (factor-1);
} else {
lastScaleFactor *= factor;
}
}
}
//---handle rotate gesture---
-(IBAction) handleRotateGesture:(UIGestureRecognizer *) sender {
CGFloat rotation = [(UIRotationGestureRecognizer *) sender rotation];
CGAffineTransform transform = CGAffineTransformMakeRotation(rotation + netRotation);
sender.view.transform = transform;
if (sender.state == UIGestureRecognizerStateEnded) {
netRotation += rotation;
}
}
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这是一个相当古老的线程,我遇到了这个 imageview 子类,它非常适合缩放、旋转和平移。它在图像视图上使用手势识别器。我正在将其用于我的应用程序之一。
ZoomRotatePanImageView
I know this is a pretty old thread, I came across this imageview subclass, which works nice for zoom, rotate and pan. It uses gesture recognizer on an imageview. I am using this for one of my app.
ZoomRotatePanImageView
当您使用 CGAffineTransformMakeScale 时,每次使用都会重置 Identity 的转换,并且会丢失之前的转换信息。
尝试使用 CGAffineTransformScale(view.transform,scale,scale) 进行捏缩放。不过,您需要保留原始帧大小以控制缩放。
请参阅:如何我使用捏缩放(UIPinchGestureRecognizer)来更改 UITextView 的宽度?
对于旋转类似:
When you use CGAffineTransformMakeScale, you are resetting the transformation of Identity every time you use it and you lose the previous transformation information.
Try using
CGAffineTransformScale(view.transform,scale, scale)
for the pinch zooming. You will need to retain the original frame size to keep the zooming under control though.see: How can I use pinch zoom(UIPinchGestureRecognizer) to change width of a UITextView?
For rotation similarly:
我在斯坦福大学网站上找到了您可能感兴趣的内容:
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2010-winter
在此网站上,您需要向下滚动,直到看到数字14:“标题:讲座 #14 - MultiTouch”
下载:“14_MultiTouchDemo.zip”
在此示例中,您可以同时缩放和旋转每个图像。
希望我有帮助:)
I found something that may interest you on the stanford university website:
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2010-winter
on this site you will need to scroll down until you see the number 14: "Title: Lecture #14 - MultiTouch"
Download the: "14_MultiTouchDemo.zip"
In this example you can scale and rotate every image at the same time.
hope i helped :)
只需实现 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 在您的委托中。
我设置了一个
UIPinchGestureRecognizer
、一个UIPanGestureRecognizer
和一个UIRotationGestureRecognizer
,我希望它们同时工作。我还有一个UITapGestureRecognizer
,我不希望同时被识别。我所做的只是这样:Just implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: in your delegate.
I have a
UIPinchGestureRecognizer
, aUIPanGestureRecognizer
and aUIRotationGestureRecognizer
set up and I want them all to work at the same time. I also have aUITapGestureRecognizer
which I do not want to be recognized simultaneously. All I did was this:希望这对你有帮助,这就是我通常实现手势识别器的方式:
旋转方法:应用后将手势识别器的旋转重置为 0,因此下一个回调是当前旋转的增量 缩放
方法,最后重置手势识别器的缩放应用后为 1,因此下一个回调是当前比例的增量
确保特定视图上的捏合、平移和旋转手势识别器都可以同时识别 防止其他手势识别器同时识别
比例和旋转变换是相对于视图应用的图层的锚点此方法在用户手指之间移动手势识别器视图的锚点
Hope this can be helpful to you, that's how I usually implement gesture recognizers:
Rotate method: Reset the gesture recognizer's rotation to 0 after applying so the next callback is a delta from the current rotation
Scale Method, at the end reset the gesture recognizer's scale to 1 after applying so the next callback is a delta from the current scale
Than ensure that the pinch, pan and rotate gesture recognizers on a particular view can all recognize simultaneously prevent other gesture recognizers from recognizing simultaneously
Scale and rotation transforms are applied relative to the layer's anchor point this method moves a gesture recognizer's view's anchor point between the user's fingers