zoukankan      html  css  js  c++  java
  • 自定义Button,并实现禁止快速点击

    一、一般在开发工程中,我们都会要求处理快速多次点击按钮的操作 ,实际上只能点击一次 ,但是不能自我想象,所有的用户都会

    按照自己的想法去操作一个功能,所以我们要禁止按钮的快速多次点击

    二、实现方法

      思路一:实现方法很简单,就是在点击button的时候,把btn的enable属性设置为NO,然后再调用

    performSelector: withObject: afterDelay: 这个方法去讲btn 的状态激活,这样就要求每一个用到按钮的地方都要这么做,非常繁琐

      思路二:定义一个通用的btn的基类,然后检测自身,在什么时候会被点击,然后再做修改,但是这样就由涉及到一个问题 ,怎么监测自身在什么时候被点击了--不方便

      思路三:自定义一个继承自UIButton的类、然后重写父类的方法、尝试了一下,可行,使用非常方便,重写的方法是

    - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

    见效果图

    贴出代码共参考 

    //
    //  CommonButton.h
    //  CommonButton
    //
    //  Created by pk on 14/12/24.
    //  Copyright (c) 2014年 pk. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface CommonButton : UIButton
    
    @end
    //
    //  CommonButton.m
    //  CommonButton
    //
    //  Created by pk on 14/12/24.
    //  Copyright (c) 2014年 pk. All rights reserved.
    //
    
    #import "CommonButton.h"
    
    @implementation CommonButton
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    
    
    - (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
    {
        [super sendAction:action to:target forEvent:event];
        self.enabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.enabled = YES;
        });
    }
    
    @end

    是不是很方便

  • 相关阅读:
    EMC、Pure和NetApp推新品,NAS闪存场景在哪里
    Tomcat 开启Gzip压缩
    win10+ubuntu双系统安装方案
    游戏中水的渲染技术系列一
    什么时候用到线程
    高并发和多线程
    angularJS双向绑定和依赖反转
    javascript ES6
    angularJS核心原理
    javascript限定输入textarea输入长度
  • 原文地址:https://www.cnblogs.com/tianlin106/p/4183640.html
Copyright © 2011-2022 走看看