iOS 防止UIButton连续点击

版权声明:禁止转载、复制 https://blog.csdn.net/qq_37191821/article/details/84671849

写一个UIButton的分类:

当点击之后1.5秒方能再次点击响应点击事件

#import "UIButton+Event.h"

@implementation UIButton (Event)

/** 重写父类方法*/

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

    // 响应父类方法

    [super sendAction:action to:target forEvent:event];

    // 设置按钮属性不能相应事件

    [self setUserInteractionEnabled:false];

    // GCD 延时执行

    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5/*延迟执行时间*/ * NSEC_PER_SEC));

    dispatch_after(delayTime, dispatch_get_main_queue(), ^{

        // 设置按钮属性能相应事件

        [self setUserInteractionEnabled:true];

    });

}

@end

猜你喜欢

转载自blog.csdn.net/qq_37191821/article/details/84671849
今日推荐