Вопрос

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!

Это было полезно?

Решение

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 .

Другие советы

-(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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top