先上OC版本
// UIButton+Touch.h
// button
//
// Created by 马耀 on 16/5/3.
// Copyright © 2016年 mayao. All rights reserved.
//
#import <UIKit/UIKit.h>
#define defaultInterval 0.5 //默认时间间隔
@interface UIButton (touch)
/**设置点击时间间隔*/
@property (nonatomic, assign) NSTimeInterval timeInterval;
@end
//// UIButton+Touch.m
// button
//
// Created by 马耀 on 16/5/3.
// Copyright © 2016年 mayao. All rights reserved.
//
#import "UIButton+Touch.h"
#import <UIKit/UIKit.h>
@interface UIButton()
/**bool 类型 设置是否执行点UI方法*/
@property (nonatomic, assign) BOOL isIgnoreEvent;
@end
@implementation UIButton (touch)
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selA = @selector(sendAction:to:forEvent:);
SEL selB = @selector(mySendAction:to:forEvent:);
Method methodA = class_getInstanceMethod(self,selA);
Method methodB = class_getInstanceMethod(self, self);
BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
if (isAdd) {
class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
}else{
method_exchangeImplementations(methodA, methodB);
}
});
}
- (NSTimeInterval)timeInterval
{
return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setTimeInterval:(NSTimeInterval)timeInterval
{
objc_setAssociatedObject(self, @selector(timeInterval), @(timeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
if (self.isIgnoreEvent == 0) {
self.timeInterval = defaultInterval;
};
if (self.isIgnoreEvent) return;
if (self.timeInterval > 0)
{
self.isIgnoreEvent = YES;
[self performSelector:@selector(setIsIgnoreEvent:) withObject:@(NO) afterDelay:self.timeInterval];
}
}
[self mySendAction:action to:target forEvent:event];
}
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent{
objc_setAssociatedObject(self, @selector(isIgnoreEvent), @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnoreEvent{
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
@end
mark swift 版本
//
// hhh.swift
// button
//
// Created by 马耀 on 16/5/4.
// Copyright © 2016年 mayao. All rights reserved.
//
import UIKit
/// 默认时间间隔
var defaultIntervalL:Double = 0.5
/// 延时时间key
private var buttonDelayedTime = "buttonDelayedTime"
/// 是否可以点击key
private var buttonIsIgnoreEvent = "buttonIsIgnoreEvent"
extension UIButton{
/// 延时时间
public var timeInterval:NSTimeInterval {
get{
if(objc_getAssociatedObject(self, &buttonDelayedTime) == nil){
objc_setAssociatedObject(self, &buttonDelayedTime, 0,.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return 0
}else{
return objc_getAssociatedObject(self,&buttonDelayedTime).doubleValue
}
}
set{
objc_setAssociatedObject(self, &buttonDelayedTime, newValue,.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
/// 是否可以点击 禁止调用
private var isIgnoreEvent:Bool {
get{
if(objc_getAssociatedObject(self, &buttonIsIgnoreEvent) == nil){
objc_setAssociatedObject(self, &buttonIsIgnoreEvent, true,.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return false
}else{
return objc_getAssociatedObject(self, &buttonIsIgnoreEvent).boolValue
}
}
set{
objc_setAssociatedObject(self, &buttonIsIgnoreEvent, newValue,.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
public override class func initialize(){
struct Static{
static var token:dispatch_once_t = 0
}
if self != UIButton.self{
return
}
dispatch_once(&Static.token, {
_ in
let selA = Selector("sendAction:to:forEvent:")
let selB = Selector("mySendAction:target:event:")
let methodA = class_getInstanceMethod(self,selA);
let methodB = class_getInstanceMethod(self,selB);
let isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
if (isAdd) {
class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
}else{
method_exchangeImplementations(methodA, methodB);
}
})
}
public func mySendAction(action:Selector,target:NSObject,event:UIEvent){
if(NSStringFromClass(self.classForCoder) == "UIButton"){
if (self.isIgnoreEvent){
return
}else{
self.isIgnoreEvent = true
}
self.mySendAction(action, target: target, event: event)
if (self.timeInterval == 0)
{
self.delay(defaultIntervalL) { () -> () in
self.isIgnoreEvent = false
}
}else{
self.delay(timeInterval) { () -> () in
self.isIgnoreEvent = false
}
}
}
}
}
public extension NSObject {
/**
在延迟后结束. 在 main_queue 调用.
- parameter delay: 延迟的秒数
*/
func delay(delay: Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
}