获取UIPinchGestureRecognizer手指位置

时间:2013-07-15 15:09:27

标签: iphone ios position cgaffinetransform uipinchgesturerecognizer

我正在使用UIGestureRecognizer来完美地转换视图及其工作,但现在我想用它来独立地转换视图的宽度和高度。我想到解决这个问题的唯一方法就是获得两个手指位置并制作一个if子句来识别用户是否正在尝试增加宽度或高度,但为此我需要让每个手指位置都参与捏手势。但我无法找到任何方法来做到这一点我想知道这是否可行或是否有另一种方法来实现这一点。

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

}

1 个答案:

答案 0 :(得分:8)

如果有人需要这样做,得到了答案,有一种称为触摸位置的方法。使用此方法,您可以获得每个触摸x和y位置。但是当手势识别器状态开始时调用它,因为如果你在状态发生变化时它会崩溃。将这些值保存在一些变量中,你就可以了。希望它可以帮助有兴趣的人。

- (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){

}