質問

I have a image view and want to add some custom pinch gesture recognizers. I'm able to zoom my image view, but the problem is, it is not zooming in from the center of the two fingers.

How can I zoom from center of the two fingers? This is what I'm currently doing (in viewDidLoad)

UIPinchGestureRecognizer* pinchRecognizer = 
    [[UIPinchGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(handlePinch:)];    
 [imageView addGestureRecognizer:pinchRecognizer]; 

here is the code for pinch method

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{  

    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;

}

Thanx for the rep guys...now here i'm updating my question.. i want to zoom image on button click..What i'm doing is

-(IBAction)Zoom_image:(id)sender {

 CGFloat scaleValue = 2;

 CGAffineTransform transform = GAffineTransformMakeScale(scaleValue,scaleValue);

 self.backgroundImgView.transform = transform;

}

役に立ちましたか?

解決

This will also work. Just move the image to the center of the pinch, scale it and then back to its position in just one transform.

- (void) pinch:(UIPinchGestureRecognizer *) recognizer {

    CGPoint anchor = [recognizer locationInView:imageToScale];
    anchor = CGPointMake(anchor.x - imageToScale.bounds.size.width/2, anchor.y-imageToScale.bounds.size.height/2);

    CGAffineTransform affineMatrix = imageToScale.transform;
    affineMatrix = CGAffineTransformTranslate(affineMatrix, anchor.x, anchor.y);
    affineMatrix = CGAffineTransformScale(affineMatrix, [recognizer scale], [recognizer scale]);
    affineMatrix = CGAffineTransformTranslate(affineMatrix, -anchor.x, -anchor.y);
    imageToScale.transform = affineMatrix;

    [recognizer setScale:1];
}

他のヒント

The problem is that all you are doing is scaling the view.

You make no attempt to translate the zoom to be centered about the center of the two finger points.

So grab the the two finger points. Work out their center and add a translate.

Note that the code below will show in principle what you need to do, I can guarantee that it will need to be fixed as Affine Transforms in practise are not trivial and im not your bug fixer.

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
    UIView *view = recognizer.view;

    CGRect frame = view.bounds;

    //you may wish to get more accurate by querying and calculating against the touches via 
    //- (NSUInteger)numberOfTouches;
    //- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;
    CGPoint nominalCenter = [recognizer locationInView:view];

    CGFloat deltaFromCenterX = CGRectGetMidX(frame) - nominalCenter.x;

    CGFloat deltaFromCenterY = CGRectGetMidY(frame) - nominalCenter.y;

    currentScale = currentScale * recognizer.scale;

    CGAffineTransform scale = CGAffineTransformMakeScale(currentScale, currentScale);

    CGAffineTransform translate = CGAffineTransformMakeTranslation(deltaFromCenterX, deltaFromCenterY);

    //possibly its going to be scale then translate instead
    CGAffineTransform final = CGAffineTransformConcat(translate, scale);

    recognizer.view.transform = final;
    recognizer.scale = 1;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top