【问题标题】:UIImageView Gestures (Zoom, Rotate) QuestionUIImageView 手势(缩放、旋转)问题
【发布时间】:2011-03-27 18:58:59
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • 我正在尝试在我的应用程序上设置缩放缩放,您能告诉我代码中的 lastScaleFactor 是什么吗?提前谢谢你。

标签: iphone uiimageview zooming rotation pinch


【解决方案1】:

我知道这是一个非常古老的线程,我遇到了这个 imageview 子类,它适用于缩放、旋转和平移。它在图像视图上使用手势识别器。我将它用于我的一个应用程序。

ZoomRotatePanImageView

【讨论】:

  • 非常感谢,这正是我想要的。
【解决方案2】:

只需在您的委托中实现gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

我有一个UIPinchGestureRecognizer、一个UIPanGestureRecognizer 和一个UIRotationGestureRecognizer 设置,我希望它们都同时工作。我也有一个UITapGestureRecognizer,我确实希望同时被识别。我所做的只是:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && ![otherGestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        return YES;
    }

    return NO;
}

【讨论】:

    【解决方案3】:

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

    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,因此下一个回调是当前旋转的增量

    - (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,因此下一个回调是当前比例的增量

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

    确保特定视图上的捏合、平移和旋转手势识别器都可以同时识别,以防止其他手势识别器同时识别

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

    相对于图层的锚点应用缩放和旋转变换此方法在用户手指之间移动手势识别器视图的锚点

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

    【讨论】:

    • 它节省了我一整天的时间。谢谢!!
    【解决方案4】:

    当您使用 CGAffineTransformMakeScale 时,您每次使用它时都会重置 Identity 的转换,并且您会丢失之前的转换信息。

    尝试使用CGAffineTransformScale(view.transform,scale, scale) 进行捏合缩放。不过,您需要保留原始帧大小以控制缩放。
    见:How can I use pinch zoom(UIPinchGestureRecognizer) to change width of a UITextView?

    对于旋转类似:

       if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
            view.transform = CGAffineTransformRotate([view transform], [gestureRecognizer rotation]);
            [gestureRecognizer setRotation:0];
        }
    

    【讨论】:

      【解决方案5】:

      我在 斯坦福大学 网站上找到了您可能感兴趣的内容:

      http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2010-winter

      在此站点上,您需要向下滚动直到看到数字 14:“标题:Lecture #14 - MultiTouch”

      下载:“14_MultiTouchDemo.zip”

      在此示例中,您可以同时缩放和旋转每张图像。

      希望我能帮上忙 :)

      【讨论】:

      • 这个demo没有使用UIGestureRecognizer,它解决了touches的含义。
      猜你喜欢
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多