美文网首页selector
iOS UIButton 防止连续点击(最后一次才去响应事件)

iOS UIButton 防止连续点击(最后一次才去响应事件)

作者: 马叔叔 | 来源:发表于2020-07-01 01:35 被阅读0次

    方案一

    - (void)buttonClickMethod:(UIButton *)sender {
        
        // 每次点击的时候都会先取消一次响应,然后调用perform方法,延迟响应该事件,避免多次响应
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayResponseMethod) object:btn];
        // 延迟执行,一般设置1秒左右,太久会显得有延迟,响应也不能太慢
        [self performSelector:@selector(delayResponseMethod) withObject:btn afterDelay:1.0];
    }
    - (void)delayResponseMethod {
        NSLog(@"延迟执行的方法");
    }
    

    方案二

    • 通过NSTimer延迟执行方法,和方案一类似
    //定时器
    @property (nonatomic, strong) NSTimer *timer;//定时器
    @property(nonatomic, assign) NSInteger count;
    //需要给count一个默认值,比如0
    - (void)buttonClickMethod:(UIButton *)sender {
        self.count ++;
        [self.timer invalidate];
        self.timer = nil;
        self.timer =[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(delayResponseMethod) userInfo:nil repeats:NO];
    /* NSRunLoopCommonModes 防止滚动的时候有延迟*/
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
    - (void)delayResponseMethod {
        NSLog(@"延迟执行的方法");
    }
    

    方案三

    通过Runtime交换UIButton的响应事件方法,从而控制响应事件的时间间隔。

    1 创建一个UIButton的分类,使用runtime增加public属性cs_eventInterval和private属性cs_eventInvalid。
    2 在+load方法中使用runtime将UIButton的-sendAction:to:forEvent:方法与自定义的cs_sendAction:to:forEvent:方法进行交换
    3 使用cs_eventInterval作为控制cs_eventInvalid的计时因子,用cs_eventInvalid控制UIButton的event事件是否有效。
    *代码实现如下

    @interface UIButton (Extension)

    /** 时间间隔 */
    @property(nonatomic, assign)NSTimeInterval cs_eventInterval;

    @end

    import "UIButton+Extension.h"

    import <objc/runtime.h>

    static char *const kEventIntervalKey = "kEventIntervalKey"; // 时间间隔
    static char *const kEventInvalidKey = "kEventInvalidKey"; // 是否失效

    @interface UIButton()

    /** 是否失效 - 即不可以点击 */
    @property(nonatomic, assign)BOOL cs_eventInvalid;

    @end

    @implementation UIButton (Extension)

    • (void)load {
      // 交换方法
      Method clickMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
      Method cs_clickMethod = class_getInstanceMethod(self, @selector(cs_sendAction:to:forEvent:));
      method_exchangeImplementations(clickMethod, cs_clickMethod);
      }

    pragma mark - click

    • (void)cs_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
      if (!self.cs_eventInvalid) {
      self.cs_eventInvalid = YES;
      [self cs_sendAction:action to:target forEvent:event];
      [self performSelector:@selector(setCs_eventInvalid:) withObject:@(NO) afterDelay:self.cs_eventInterval];
      }
      }

    pragma mark - set | get

    • (NSTimeInterval)cs_eventInterval {
      return [objc_getAssociatedObject(self, kEventIntervalKey) doubleValue];
      }

    • (void)setCs_eventInterval:(NSTimeInterval)cs_eventInterval {
      objc_setAssociatedObject(self, kEventIntervalKey, @(cs_eventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
      }

    • (BOOL)cs_eventInvalid {
      return [objc_getAssociatedObject(self, kEventInvalidKey) boolValue];
      }

    • (void)setCs_eventInvalid:(BOOL)cs_eventInvalid {
      objc_setAssociatedObject(self, kEventInvalidKey, @(cs_eventInvalid), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
      }
      测试代码如下
      /** 方法三 */

    • (void)drawExpecialBtn{
      UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
      [btn setTitle:@"按钮点击" forState:UIControlStateNormal];
      [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
      // 按钮不可点击时,文字颜色置灰
      [btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
      [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
      btn.center = self.view.center;
      [btn addTarget:self action:@selector(tapBtn:) forControlEvents:UIControlEventTouchUpInside];
      btn.cs_eventInterval = 2.0;
      [self.view addSubview:btn];
      }

    • (void)tapBtn:(UIButton *)btn {
      NSLog(@"按钮点击...");
      }

    • 在方法三中交互UIButton的sendAction:to:forEvent:方法,实际上交互的是UIControl的sendAction:to:forEvent:方法,所以在使用·UIControl·或其·子类(比如UISlider)·的·sendAction:to:forEvent:·方法时会引起参数缺失的崩溃。

    相关文章

      网友评论

        本文标题:iOS UIButton 防止连续点击(最后一次才去响应事件)

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