当前位置:编程学习 > wap >>

IOS开发(72)之监听捕获轻击的手势动作

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}

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,