Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UIPinchGestureRecognizer finger positions

Im using the UIGestureRecognizer to transform a view and its workin perfectly, but now I want to use it to transform my view width and height independently. The only way that comes to my mind to solve this is getting the two finger positions and make an if clause to recognize if the user is trying to increase width or height, but for this I need to get each finger position involved in the Pinch Gesture. But I cant find any method to do this I was wondering if this is posible or if there is another alternative for achieving this.

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


        recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, 1);//To transform  height insted of width just swap positions of the second and third parameter. 
        NSLog(@"%f",recognizer.scale);
        recognizer.scale = 1;

}
like image 728
Lord Pepito Avatar asked Jul 15 '13 15:07

Lord Pepito


1 Answers

Got the answer if somebody needs to do this, there is a method called location of touch. With this method you can get each touch x and y position. But call it when the Gesture recognizer state began because it crashes if you do it in the state changed. Save this values in some variables and you are good to go. Hope it helps someone who is interested.

- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
 if(recognizer.state == UIGestureRecognizerStateBegan){
            NSLog(@"pos : 0%f, %f",[recognizer locationOfTouch:0 inView:self.view].x,[recognizer locationOfTouch:0 inView:self.view].y);
            NSLog(@"pos 1: %f, %f",[recognizer locationOfTouch:1 inView:self.view].x,[recognizer locationOfTouch:1 inView:self.view].y);
        }
        if(recognizer.state == UIGestureRecognizerStateChanged){
            recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, 1);
            //NSLog(@"%f",recognizer.scale);
            recognizer.scale = 1;
        }
        if(recognizer.state == UIGestureRecognizerStateEnded){

}       
like image 54
Lord Pepito Avatar answered Oct 22 '22 09:10

Lord Pepito