hoya21
hoya21

Reputation: 893

Pinch and Tap Gestures don't work properly

I have an ImageView into a ViewController and i want to zoom in or zoom out with Pinch Gesture. Also , i want to get the initial view when the user double taps the image.I followed this tutorial Make Gallery

If i use only pinch gesture all work properly, i can zoom in until max zoom achieved and i can zoom out until min zoom achieved.The right is result is in the picture below. right result

Although, if i double tap the ImageView the ImageView gets its initial format (Image 1) but the achievement of min zoom isn't recognized and if i continue with pinch gesture to zoom out the ImageView continues to zoom out and i get this result (Image 2): wrong result

These are my methods that handle the events:

@interface ImageGalleryViewController (){


NSString *productName;
CGFloat previousScale;
CGFloat previousRotation;
CGFloat beginX;
CGFloat beginY;

CGFloat scale;
//CGFloat __previousScale;
}

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.

   scale=1.0;

   UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]       initWithTarget:self action:@selector(scaleImage:)];
   [self.view addGestureRecognizer:pinchGesture];

   UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetImage:)];
   tapGesture.numberOfTapsRequired=2;
   [self.view addGestureRecognizer:tapGesture];


}

 - (void)resetImage:(UITapGestureRecognizer *)recognizer{
     [UIView animateWithDuration:0.3 animations:^() {

     self.view.transform = CGAffineTransformIdentity;

     }];
     self.bigImage.transform = CGAffineTransformIdentity;
     [self.bigImage setCenter: CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2+32)];
     [UIView commitAnimations];
 }

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

     NSLog(@"Scale: %f", [recognizer scale]);
     if ([recognizer state] == UIGestureRecognizerStateBegan) {
        previousScale = scale;
     }

     CGFloat currentScale = MAX(MIN([recognizer scale] * scale, 10), 1);
     CGFloat scaleStep = currentScale / previousScale;
     [self.view setTransform: CGAffineTransformScale(self.view.transform, scaleStep, scaleStep)];

    previousScale = currentScale;

    if ([recognizer state] == UIGestureRecognizerStateEnded ||
        [recognizer state] == UIGestureRecognizerStateCancelled ||
        [recognizer state] == UIGestureRecognizerStateFailed) {
    // Gesture can fail (or cancelled?) when the notification and the object is dragged simultaneously
        scale = currentScale;
        NSLog(@"Final scale: %f", scale);
    }
}

So how can i stop zoom out if the initial right view (first image) has achieved?

Upvotes: 1

Views: 491

Answers (1)

hoya21
hoya21

Reputation: 893

I had to reset variable scale to 1.0 into resetImage method. So now it works properly.

Upvotes: 1

Related Questions