开发进阶 Runtime

行代码快速集成按钮延时处理 (VIA)

2018-01-04  本文已影响11人  其实也没有

[iOS]1 行代码快速集成按钮延时处理(hook 实战)

01. 按钮延时处理事件有什么应用场景?

02.实例分析?

像下面的demo里写的这样:

<figure style="display: block; margin: 2.7rem auto; text-align: center;"> JPBtnClickDelay

<figcaption style="display: block; text-align: center; font-size: 1rem; line-height: 2.7rem; color: rgb(144, 144, 144);"></figcaption>

</figure>

收藏这类功能的事件链是:用户点击-->处理点击 -->发送请求

03、动态添加方法和属性(hook)?

03.1 runtime是什么?

03.2 动态添加方法和属性是什么?

在setter方法里使用runtime的以下方法动态添加属性。

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

在getter方法里使用runtime的以下方法动态获取属性值。

id objc_getAssociatedObject(id object, const void *key)

03.3 方法交换是什么?

<figure style="display: block; margin: 2.7rem auto; text-align: center;">[图片上传失败...(image-b9500d-1515038406504)]

<figcaption style="display: block; text-align: center; font-size: 1rem; line-height: 2.7rem; color: rgb(144, 144, 144);"></figcaption>

</figure>

<figure style="display: block; margin: 2.7rem auto; text-align: center;">[图片上传失败...(image-156db5-1515038406504)]

<figcaption style="display: block; text-align: center; font-size: 1rem; line-height: 2.7rem; color: rgb(144, 144, 144);"></figcaption>

</figure>

04.思路分析?

我们知道 UIButton 继承自 UIControlUIButton 的所有处理事件的能力都是它的父类 UIControl 传给它的。UIControl 有这样一个方法:

// send the action. the first method is called for the event and is a point at which you can observe or override behavior. it is called repeately by the second.
- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;

官方的解释翻译过来是这样的:这个方法用以传递事件消息,是监听到事件后最先调用的方法,并且它是随着事件的重复产生而频繁调用的。

所以我们要实现拦截事件传递,重写这个方法是最优解。

05.代码实现?

#import <UIKit/UIKit.h>

@interface UIControl (JPBtnClickDelay)

/** 延迟时间 */
@property(nonatomic)NSTimeInterval jp_acceptEventInterval;

@end

#import "UIControl+JPBtnClickDelay.h"
#import <objc/runtime.h>

@interface UIControl ()

/** 是否忽略点击 */
@property(nonatomic)BOOL jp_ignoreEvent;

@end

@implementation UIControl (JPBtnClickDelay)

-(void)jp_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if (self.jp_ignoreEvent) return;

    if (self.jp_acceptEventInterval > 0) {
        self.jp_ignoreEvent = YES;
        [self performSelector:@selector(setJp_ignoreEvent:) withObject:@(NO) afterDelay:self.jp_acceptEventInterval];
    }
    [self jp_sendAction:action to:target forEvent:event];
}

-(void)setJp_ignoreEvent:(BOOL)jp_ignoreEvent{
    objc_setAssociatedObject(self, @selector(jp_ignoreEvent), @(jp_ignoreEvent), OBJC_ASSOCIATION_ASSIGN);
}

-(BOOL)jp_ignoreEvent{
    return [objc_getAssociatedObject(self, _cmd, boolValue];
}

-(void)setJp_acceptEventInterval:(NSTimeInterval)jp_acceptEventInterval{
    objc_setAssociatedObject(self, @selector(jp_acceptEventInterval), @(jp_acceptEventInterval), OBJC_ASSOCIATION_ASSIGN);
}

-(NSTimeInterval)jp_acceptEventInterval{
    return [objc_getAssociatedObject(self, _cmd) doubleValue];
}

+(void)load{
    Method sys_Method = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));

    Method add_Method = class_getInstanceMethod(self, @selector(jp_sendAction:to:forEvent:));

    method_exchangeImplementations(sys_Method, add_Method);
}

@end

06. 分类的使用?

这里有两个 UIButton 的实例对象:

[self.normalBtn addTarget:self action:@selector(normalBtnClick) forControlEvents:UIControlEventTouchUpInside];

[self.delayBtn addTarget:self action:@selector(delayBtnClick) forControlEvents:UIControlEventTouchUpInside];
self.delayBtn.jp_acceptEventInterval = 1.0f;

07. Demo下载?

请点击这里去往Github

NewPan 的文章集合

下面这个链接是我所有文章的一个集合目录。这些文章凡是涉及实现的,每篇文章中都有 Github 地址,Github 上都有源码。

NewPan 的文章集合索引

如果你有问题,除了在文章最后留言,还可以在微博 @盼盼_HKbuy 上给我留言,以及访问我的 Github

上一篇下一篇

猜你喜欢

热点阅读