[英]UIImageView Gestures (Zoom, Rotate) Question
I would like to make 2 operations to an UIImageView
zoom, rotate, I have 2 problems: 我想对
UIImageView
进行2次操作变焦,旋转,我有2个问题:
A. I make an operation for zoom for ex.
答:我为ex做了缩放操作。 and when I try to make rotation the
UIImageView
is set to initial size, I would like to know how to keep the zoomedUIImageView
and make the rotation from the zoomed image.当我尝试旋转
UIImageView
设置为初始大小时,我想知道如何保持缩放的UIImageView
并从缩放的图像进行旋转。B. I would like to combine the zoom operation with rotation and I don't know ho to implement this:
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;
}
}
Thanks 谢谢
Hope this can be helpful to you, that's how I usually implement gesture recognizers: 希望这对您有所帮助,这就是我通常如何实现手势识别器:
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePiece:)];
[piece addGestureRecognizer:rotationGesture];
[rotationGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
[pinchGesture setDelegate:self];
[piece addGestureRecognizer:pinchGesture];
[pinchGesture release];
Rotate method: Reset the gesture recognizer's rotation to 0 after applying so the next callback is a delta from the current rotation 旋转方法:应用后将手势识别器的旋转重置为0,以便下一个回调是当前旋转的增量
- (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer {
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
[gestureRecognizer setRotation:0];
}
}
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 缩放方法,最后在应用后将手势识别器的比例重置为1,因此下一个回调是当前比例的增量
- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer {
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
[gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
[gestureRecognizer setScale:1];
}
}
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 确保特定视图上的捏合,平移和旋转手势识别器可以同时识别,同时防止其他手势识别器同时识别
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// if the gesture recognizers are on different views, don't allow simultaneous recognition
if (gestureRecognizer.view != otherGestureRecognizer.view)
return NO;
// if either of the gesture recognizers is the long press, don't allow simultaneous recognition
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
return NO;
return YES;
}
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 相对于图层的锚点应用缩放和旋转变换此方法移动手势识别器的视图在用户手指之间的锚点
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
Just implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: in your delegate. 只需在你的委托中实现gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
I have a UIPinchGestureRecognizer
, a UIPanGestureRecognizer
and a UIRotationGestureRecognizer
set up and I want them all to work at the same time. 我有一个
UIPinchGestureRecognizer
,一个UIPanGestureRecognizer
和一个UIRotationGestureRecognizer
设置,我希望它们能同时工作。 I also have a UITapGestureRecognizer
which I do not want to be recognized simultaneously. 我也有一个
UITapGestureRecognizer
,我不希望被认可的同时。 All I did was this: 我所做的就是:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && ![otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return YES;
}
return NO;
}
I found something that may interest you on the stanford university website: 我在斯坦福大学的网站上找到了你可能感兴趣的东西:
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2010-winter 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" 在这个网站上你需要向下滚动,直到看到数字14:“ 标题:第14讲 - MultiTouch”
Download the: "14_MultiTouchDemo.zip" 下载: “14_MultiTouchDemo.zip”
In this example you can scale and rotate every image at the same time. 在此示例中,您可以同时缩放和旋转每个图像。
hope i helped :) 希望我帮助:)
When you use CGAffineTransformMakeScale, you are resetting the transformation of Identity every time you use it and you lose the previous transformation information. 当您使用CGAffineTransformMakeScale时,每次使用它时都会重置Identity的转换,并且您将丢失先前的转换信息。
Try using CGAffineTransformScale(view.transform,scale, scale)
for the pinch zooming. 尝试使用
CGAffineTransformScale(view.transform,scale, scale)
进行缩放缩放。 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? 请参阅: 如何使用缩放缩放(UIPinchGestureRecognizer)来更改UITextView的宽度?
For rotation similarly: 对于轮换类似:
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformRotate([view transform], [gestureRecognizer rotation]);
[gestureRecognizer setRotation:0];
}
I know this is a pretty old thread, I came across this imageview subclass, which works nice for zoom, rotate and pan. 我知道这是一个非常古老的线程,我遇到了这个imageview子类,它适用于缩放,旋转和平移。 It uses gesture recognizer on an imageview.
它在imageview上使用手势识别器。 I am using this for one of my app.
我正在使用这个用于我的应用程序之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.