UIPinchGestureRecognizer

UIPinchGestureRecognizer

Possible Duplicate:
Using UIPinchGestureRecognizer to scale uiviews in single direction




我的代码如下:

 UIPinchGestureRecognizer *croperViewGessture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(croperViewScale:)];
        croperViewGessture.delegate=self;
        [croperView addGestureRecognizer:croperViewGessture];

     -(void)CanvasScale:(id)sender
{
    if([(UIPinchGestureRecognizer *)sender state]==UIGestureRecognizerStateBegan)
    {
        if ([sender numberOfTouches] == 2) {
            _pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
            _pntOrig[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];
        } else {
            _pntOrig[0] = [(UIPinchGestureRecognizer *)sender locationInView:cropedAngle];
            _pntOrig[1] = _pntOrig[0];
        }
        _lenOrigX = fabs(_pntOrig[1].x - _pntOrig[0].x);
        _lenOrigY = fabs(_pntOrig[1].y - _pntOrig[0].y);
        _xScale = 1.0;
        _yScale = 1.0;
    }
    if ([(UIPinchGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged) {
        if ([sender numberOfTouches] == 2) {
            CGPoint pntNew[2];
            pntNew[0] = [(UIPinchGestureRecognizer *)sender locationOfTouch:0 inView:cropedAngle];
            pntNew[1] = [(UIPinchGestureRecognizer *)sender locationOfTouch:1 inView:cropedAngle];

            CGFloat lenX = fabs(pntNew[1].x - pntNew[0].x);
            CGFloat lenY = fabs(pntNew[1].y - pntNew[0].y);

            CGFloat dX = fabs(lenX - _lenOrigX);
            CGFloat dY = fabs(lenY - _lenOrigY);
            CGFloat tot = dX + dY;

            CGFloat pX = dX / tot;
            CGFloat pY = dY / tot;

            CGFloat scale = [(UIPinchGestureRecognizer *)sender scale];
            CGFloat dscale = scale - 1.0;
            _xScale = dscale * pX + 1;
            _yScale = dscale * pY + 1;
        }
    }
CGAffineTransform transform = cropedAngle.transform;
CGAffineTransform newTarnsform = CGAffineTransformScale(transform, _lenOrigX, _lenOrigY);
[cropedAngle setTransform:newTarnsform];


}

但是问题是,当我执行Zoomin或Zoomout时,在屏幕上的整个屏幕上展开并禁用它之后,请查看我的代码并告诉我出了什么问题。

请帮我解决这个问题
我提前谢谢。

最佳答案

我为UIPinchGestureRecognizer编写了自己的自定义扩展名,除了普通的xScale以外,还提供了yScalescale。这是UIPinchGestureRecognizer的替代品。现在,您可以选择查看普通的scale或新的xScaleyScale。相应地使用这些值可根据用户的捏合手势缩放视图。

RMPinchGestureRecognizer.h

#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

@interface RMPinchGestureRecognizer : UIPinchGestureRecognizer {
    CGPoint _pntOrig[2];
    CGFloat _lenOrigX;
    CGFloat _lenOrigY;
    CGFloat _xScale;
    CGFloat _yScale;
}

@property (nonatomic, readonly) CGFloat xScale;
@property (nonatomic, readonly) CGFloat yScale;

@end


RMPinchGestureRecognizer.m

#import "RMPinchGestureRecognizer.h"

@implementation RMPinchGestureRecognizer

@synthesize xScale = _xScale;
@synthesize yScale = _yScale;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];

    if (self.state == UIGestureRecognizerStateChanged) {
        if ([self numberOfTouches] == 2) {
            CGPoint pntNew[2];
            pntNew[0] = [self locationOfTouch:0 inView:self.view];
            pntNew[1] = [self locationOfTouch:1 inView:self.view];

            CGFloat lenX = fabs(pntNew[1].x - pntNew[0].x);
            CGFloat lenY = fabs(pntNew[1].y - pntNew[0].y);

            CGFloat dX = fabs(lenX - _lenOrigX);
            CGFloat dY = fabs(lenY - _lenOrigY);
            CGFloat tot = dX + dY;

            CGFloat pX = dX / tot;
            CGFloat pY = dY / tot;

            CGFloat scale = [self scale];
            CGFloat dscale = scale - 1.0;
            _xScale = dscale * pX + 1;
            _yScale = dscale * pY + 1;
        }
    }
}

- (void)setState:(UIGestureRecognizerState)state {
    if (state == UIGestureRecognizerStateBegan) {
        if ([self numberOfTouches] == 2) {
            _pntOrig[0] = [self locationOfTouch:0 inView:self.view];
            _pntOrig[1] = [self locationOfTouch:1 inView:self.view];
        } else {
            _pntOrig[0] = [self locationInView:self.view];
            _pntOrig[1] = _pntOrig[0];
        }
        _lenOrigX = fabs(_pntOrig[1].x - _pntOrig[0].x);
        _lenOrigY = fabs(_pntOrig[1].y - _pntOrig[0].y);
        _xScale = 1.0;
        _yScale = 1.0;
    }

    [super setState:state];
}

@end

10-08 15:40