UIImageView 手势(缩放、旋转)问题

UIImageView Gestures (Zoom, Rotate) Question(UIImageView 手势(缩放、旋转)问题)

本文介绍了UIImageView 手势(缩放、旋转)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 UIImageView 进行 2 次缩放、旋转操作,我有 2 个问题:

I would like to make 2 operations to an UIImageView zoom, rotate, I have 2 problems:

A. 我为 ex 做了一个缩放操作.当我尝试进行旋转 UIImageView 设置为初始大小时,我想知道如何保持缩放后的 UIImageView 并从缩放后的图像进行旋转.

A. I make an operation for zoom for ex. and when I try to make rotation the UIImageView is set to initial size, I would like to know how to keep the zoomed UIImageView and make the rotation from the zoomed image.

B.我想将缩放操作与旋转结合起来,但我不知道如何实现:

B. I would like to combine the zoom operation with rotation and I don't know ho to implement this:

- (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;
    }
}

谢谢

推荐答案

希望对你有所帮助,这就是我通常实现手势识别器的方式:

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 方法:应用后将手势识别器的旋转重置为 0,因此下一个回调是当前旋转的增量

Rotate method: Reset the gesture recognizer's rotation to 0 after applying so the next callback is a delta from the current rotation

- (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 方法,最后在应用后将手势识别器的比例重置为 1,因此下一个回调是当前比例的增量

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

- (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;
    }
}

这篇关于UIImageView 手势(缩放、旋转)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIImageView 手势(缩放、旋转)问题

基础教程推荐