防止瞬时多次点击

1.在每次点击时先取消之前的操作(网上看到的方法)

- (void)buttonClicked:(id)sender
{
   //这里是关键,点击按钮后先取消之前的操作,再进行需要进行的操作
   [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(buttonClicked:) object:sender];
   [self performSelector:@selector(buttonClicked: )withObject:sender afterDelay:0.2f];
}

2.点击后将按钮置为不可点击状态,几秒后恢复(也可用GCD的dispatch_after来设置间隔)

-(void)buttonClicked:(id)sender{
   self.button.enabled = NO;
   [self performSelector:@selector(changeButtonStatus) withObject:nil afterDelay:1.0f];//防止用户重复点击
}

-(void)changeButtonStatus{
    self.button.enabled = YES;
}

3  使用NSTimeInterval来设置时间间隔

NSDate *_clickDate;

- (void)btnClick:(UIButton*)btn {

    //防止重复点击代码

    if (_clickDate != nil) {

        NSTimeInterval time = [NSDate date].timeIntervalSince1970 * 1000;

        

        if ((time - _clickDate.timeIntervalSince1970 * 1000) < 500) {

            return;

        }

    }

    

    _clickDate = [NSDate date];

//点击事件业务逻辑代码

}

 

 

4 当点击界面上的btn等点击事件时,当有一个点击事件时,其他点击事件都不会触发了,使用属性exclusivetouch;它的工作原理是:当UIView的exclusiveTouch属性设置为YES时,UIView可以独占当前窗口的touch事件。在手指离开屏幕之前,其他视图都无法触发touch事件。

用法如:[[UIButton appearance] setExclusiveTouch:YES];

转载于:https://my.oschina.net/llfk/blog/1488698

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值