欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-10-27 13:56:10
1 前言 使用uitapgesturerecognizer 监听捕获用户点击了屏幕的事件。 2 代码实例 zyviewcontroller.m   [pla...

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}