zoukankan      html  css  js  c++  java
  • ios解决button重复点击的问题

    分类头文件:

    #import <UIKit/UIKit.h>

     

    @interface UIControl (CDControlCategory)

    @property(nonatomic,assign)NSTimeInterval uxy_acceptEventInterval;// 可以用这个给重复点击加间隔

    @property (nonatomic) BOOL ignoreEvent;

    @end

     

     

    ----------分类实现:----------

    #import "UIControl+HCDControlCategory.h"

    #import <objc/runtime.h>

     

    static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";

    static const char *UIControl_ignoreEvent = "UIControl_ignoreEvent";

     

     

    @implementation UIControl (HCDControlCategory)

     

    //改变两个方法的实现。在类第一次使用的时候回调用这个方法

    +(void)load{

        Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));

        Method b = class_getInstanceMethod(self, @selector(__uxy_sendAction:to:forEvent:));

        //改变两个方法的实现

        method_exchangeImplementations(a, b);

    }

    //通过关联对象重写getset方法

    - (NSTimeInterval)uxy_acceptEventInterval

    {

        return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];

    }

     

    - (void)setUxy_acceptEventInterval:(NSTimeInterval)uxy_acceptEventInterval

    {

        objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(uxy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }

    #pragma mark 现在是否可点的getset。通过关联对象。

    -(void)setIgnoreEvent:(BOOL)ignoreEvent{

        objc_setAssociatedObject(self, UIControl_ignoreEvent, @(ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }

    -(BOOL)ignoreEvent{

        return [objc_getAssociatedObject(self, UIControl_ignoreEvent) boolValue];

    }

     

     

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

    {

        if (self.ignoreEvent) return;

        if (self.uxy_acceptEventInterval > 0)

        {

            

            self.ignoreEvent = YES;

            [self performSelector:@selector(setIgnoreEvent:) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];

        }

        //调用系统实现

        [self __uxy_sendAction:action to:target forEvent:event];

    }

     

     

    @end

     

     

    ------------------ 使用 ---------------

    - (void)viewDidLoad {

        [super viewDidLoad];

     

        UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        tempBtn.frame = CGRectMake(100, 100, 30, 30);

        tempBtn.backgroundColor = [UIColor greenColor];

        [tempBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside];

        tempBtn.uxy_acceptEventInterval = 2.5;

        

        [self.view addSubview:tempBtn];

    }

     

     

    #pragma mark 这里其实就是调用我们自定义的那个方法。

    -(void)clickWithInterval:(UIButton *)sender{

        NSLog(@"你试试看。");

    }

  • 相关阅读:
    小白初学iOS中OTA部署(适应iOS7.1)
    CocoaPods管理本地库简单步骤
    CocoaPods问题记录
    UIPanGestureRecognizer下方法
    2013年度总结(反思篇)
    iOS7中cell的分割线右移的解决方法
    接触CocoaPods遇到的问题
    iOS摄像头问题
    iPhone4激活出错,菜鸟试验!!!!!
    详解UIPickerView
  • 原文地址:https://www.cnblogs.com/supersr/p/7428675.html
Copyright © 2011-2022 走看看