runtime實(shí)現(xiàn)button延時點(diǎn)擊

先上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 //默認(rèn)時間間隔

@interface UIButton (touch)

/**設(shè)置點(diǎn)擊時間間隔*/

@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 類型? 設(shè)置是否執(zhí)行點(diǎn)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

/// 默認(rèn)時間間隔

var defaultIntervalL:Double =? 0.5

/// 延時時間key

private var? buttonDelayedTime = "buttonDelayedTime"

/// 是否可以點(diǎn)擊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)

}

}

/// 是否可以點(diǎn)擊 禁止調(diào)用

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 {

/**

在延遲后結(jié)束. 在 main_queue 調(diào)用.

- parameter delay: 延遲的秒數(shù)

*/

func delay(delay: Double, closure:()->()) {

? ? ? ? dispatch_after(

? ? ? ? ? ? ? ? ?dispatch_time(

? ? ? ? ? ? ? ? ? DISPATCH_TIME_NOW,

? ? ? ? ? ? ? ? ?Int64(delay * Double(NSEC_PER_SEC))

? ? ? ?),

? ? ? ? ?dispatch_get_main_queue(), closure)

? ? ? ? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容