美文网首页
iOS 防止UIButton按钮重复点击

iOS 防止UIButton按钮重复点击

作者: JohnayXiao | 来源:发表于2017-08-10 10:08 被阅读18次
    - (void)viewDidLoad{
        [super viewDidLoad];
        
        // 1. 创建 btn 并添加点击事件
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(100, 100, 100, 60)];
        btn.backgroundColor = [UIColor greenColor];
        [btn addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
    
    // 2. 延时时间设置为 t (此处设置为1.0s) ,所以凡是前后两次点击时间间隔不超过1.0s都会被取消
    - (void)clickbtn:(UIButton *)btn{
        NSLog(@" %s ",__FUNCTION__);
        [[self class] cancelPreviousPerformRequestsWithTarget:self
                                                     selector:@selector(handleEvent:)
                                                       object:btn];
        [self performSelector:@selector(handleEvent:) withObject:btn afterDelay:1.0];
    }
    
    // 3. 点击事件的处理方法
    - (void)handleEvent:(UIButton *)btn{
        
        NSLog(@" %s ",__FUNCTION__);
    }
    

    相关文章

      网友评论

          本文标题:iOS 防止UIButton按钮重复点击

          本文链接:https://www.haomeiwen.com/subject/mqocrxtx.html