2 gaoxxxing gaoXxxing 于 2012.12.25 10:34 提问

UIPinchGestureRecognizer和UIImageView的问题

我想要通过UIPinchGestureRecognizer来对图片操作,但是代码运行之后不是很理想,而且不能缩放

代码如下:

-(void) pinching: (UIPinchGestureRecognizer *) sender {

    CGAffineTransform myTransformation =
    CGAffineTransformMakeScale(sender.scale, sender.scale);
    sender.view.transform = myTransformation;
}


- (void)viewDidLoad
{
  UIPinchGestureRecognizer *pinch =
    [[UIPinchGestureRecognizer alloc]
     initWithTarget:self
     action:@selector(pinching:)];

    pinch.delegate = self;

    [imageBG addGestureRecognizer:pinch];

    [imageBG setUserInteractionEnabled:YES];
    [imageBG setMultipleTouchEnabled:YES];
}

3个回答

darkdong
darkdong   2012.12.25 10:53
已采纳

把UIImageView放到一个UIScrollView里,并实现UIScrollVIew的代理方法viewForZoomingInScrollView返回UIImageVIew
UIScrollView还需要设置一下maximumZoomScale.

abcdeFGh_xyz
abcdeFGh_xyz   2012.12.25 11:09

在 .h文件中用变量:

CGFloat lastScale;

然后用下面的代码:

在viewWillAppear中

- (void)viewWillAppear:(BOOL)animated
{
     UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
        [pinchRecognizer setDelegate:self];
        [yourImageView addGestureRecognizer:pinchRecognizer];
}

-(void)scale:(id)sender {
    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

            lastScale = 1.0;
            return;
        }

        CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

        CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
        CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

        [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
        yourImageView.transform = newTransform

        lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
u012629497
u012629497   2016.02.19 13:36

忘了设置userInteractionEnabled

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!