iOS之单、双击事件

提供一下三种方法参考:

方法一:

//单击事件  
-(void)fun1  
{  
      
    NSLog(@"click1");  
}  
//双击事件  
-(void)fun2  
{  
    NSLog(@"click2");  
}  
  
//单击和双击方法之一  
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
{  
      
    if ([[touches anyObject] tapCount] == 1) {  
        [self performSelector:@selector(fun1) withObject:nil afterDelay:1];  
    }  
    else if ([[touches anyObject] tapCount] ==2)  
    {  
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(fun1) object:nil];  
        [self performSelector:@selector(fun2) withObject:nil afterDelay:1];  
    }  
}  

方法二:[线程]


int num = 0;  
-(void)fun1  
{  
    [NSThread sleepForTimeInterval:1];  
    if(num == 1)  
    {  
        NSLog(@"click 1");  
    }  
}  
-(void)fun2  
{  
    [NSThread sleepForTimeInterval:1];  
    if(num == 2)  
    {  
        NSLog(@"click 2");  
    }  
}  
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
{  
    if([[touches anyObject] tapCount] == 1)  
    {  
        num = 1;  
        NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(fun1) object:nil];  
        [thread start];  
    }  
    else if([[touches anyObject] tapCount] == 2)  
    {  
        num = 2;  
        NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(fun2) object:nil];  
        [thread start];  
    }  
}

方法三:[利用手势控件本身自带的方法]

原理:执行第二个方法的时候,取消第一次的方法操作

- (void)viewDidLoad  
{  
    [super viewDidLoad];     
 //点击事件  
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun1)];  
    //单点触摸  
      tap.numberOfTouchesRequired = 1;  
    //点击几次,如果是1就是单击  
    tap.numberOfTapsRequired = 1;  
    [self.view addGestureRecognizer:tap];  
      
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fun2)];  
    tap2.numberOfTapsRequired = 2;  
    [self.view addGestureRecognizer:tap2];  
      
    //如果满足第二次 第一次的就取消  
    [tap requireGestureRecognizerToFail:tap2];  
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值