你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(72)之監聽捕獲輕擊的手勢動作

IOS開發(72)之監聽捕獲輕擊的手勢動作

編輯:IOS開發綜合

1 前言
使用UITapGestureRecognizer 監聽捕獲用戶點擊了屏幕的事件。


2 代碼實例
ZYViewController.m

 

[plain]
 (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    //創建手勢 
    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)]; 
    /* 屏幕上必須有兩個手指 */ 
    self.tapGestureRecognizer.numberOfTouchesRequired = 2; 
    /* 必須敲擊3下 */ 
    self.tapGestureRecognizer.numberOfTapsRequired = 3; 
    [self.view addGestureRecognizer:self.tapGestureRecognizer]; 
     

- (void) handleTaps:(UITapGestureRecognizer*)paramSender{ 
    NSUInteger touchCounter = 0; 
    for (touchCounter = 0;touchCounter < paramSender.numberOfTouchesRequired;touchCounter++){ 
        //獲得手指位置 
        CGPoint touchPoint =[paramSender locationOfTouch:touchCounter inView:paramSender.view]; 
        NSLog(@"Touch #%lu: %@",(unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint)); 
    } 

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    //創建手勢
    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTaps:)];
    /* 屏幕上必須有兩個手指 */
    self.tapGestureRecognizer.numberOfTouchesRequired = 2;
    /* 必須敲擊3下 */
    self.tapGestureRecognizer.numberOfTapsRequired = 3;
    [self.view addGestureRecognizer:self.tapGestureRecognizer];
   
}
- (void) handleTaps:(UITapGestureRecognizer*)paramSender{
    NSUInteger touchCounter = 0;
    for (touchCounter = 0;touchCounter < paramSender.numberOfTouchesRequired;touchCounter++){
        //獲得手指位置
        CGPoint touchPoint =[paramSender locationOfTouch:touchCounter inView:paramSender.view];
        NSLog(@"Touch #%lu: %@",(unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
    }
}
運行結果


兩根手指連續三次點擊屏幕後控制台顯示


2013-05-13 15:58:36.524 UITapGestureTest[4482:c07] Touch #1: {180, 321}

2013-05-13 15:58:36.525 UITapGestureTest[4482:c07] Touch #2: {140, 119}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved