3

Im使用UIGestureRecognizer轉換視圖及其工作完美,但現在我想用它來獨立轉換視圖的寬度和高度。想到解決這個問題的唯一方法是獲取兩個手指位置並使if子句能夠識別用戶是否嘗試增加寬度或高度,但爲此,我需要將每個手指位置都包含在捏手勢中。但我無法找到任何方法來做到這一點,我想知道這是否是可行的,或者如果有另一種方法來實現這一點。獲取UIPinchGestureRecognizer的手指位置

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

} 

回答

7

得到答案如果有人需要這樣做,有一種方法稱爲觸摸位置。使用這種方法,您可以獲得每個觸摸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){ 

}  
+0

精彩的提示,謝謝! – Fattie