Question

I have one UIView which hold one UIImage . I want to apply UIPinchGesture on this UIVIew Programatically. I want to resize this UIView.

I am adding

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

and

-(void)handlePinch:(UIPinchGestureRecognizer*)sender{
}

But, I am applying it first time, so anyone help me out of this.

Help is greatly appreciated!

Was it helpful?

Solution

UIPinchGestureRecogniser has a property called scale which is the scale factor between the CGPoints of two touches. You can make use of this property.

Add this code in your handlePinch: selector method

-(void)handlePinch:(UIPinchGestureRecognizer*)sender {
    sender.view.transform = CGAffineTransformScale(sender.view.transform, 
                                                   sender.scale, 
                                                   sender.scale);
    sender.scale = 1.0;
}

As given in the documentation,

The scale value is an absolute value that varies over time. It is not the delta value from ?the last time that the scale was reported. Apply the scale value to the state of the view when the gesture is first recognized—do not concatenate the value each time the handler is called.

So we need to reset the scale value to 1.0 .

OTHER TIPS

-(void) handlePinch:(UIPinchGestureRecognizer *)gestureRecognizer{

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
        [gestureRecognizer setScale:1];
    }



}

For more details refer this link.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top