iOS 扩大 View 的点击区域, 设置点击时间间隔

  1. 如果imageView或者要点击的控件比较小, 又想把点击区域扩大, 一般都会加一个透明的UIButton, 响应UIButton的点击事件就行了, 但是比较麻烦, 这个yf_touchExtendInset属性可以直接设置view被点击的区域, 负值表示往外扩展, 正值表示向内缩小.

  2. yf_acceptEventInterval 这个属性设置被点击后, 下次能接收事件的时间间隔, 控制点击时间间隔, 比如: 设置为 2 秒, 点击一次后, 必须 2 秒后才能进行下次点击.


UIControl添加分类

#import <UIKit/UIKit.h>

@interface UIControl (YFAdd)

/// 响应区域需要改变的大小,负值表示往外扩大,正值表示往内缩小
@property (nonatomic, assign) UIEdgeInsets yf_touchExtendInset;

/// 重复点击的间隔, 隔这个  `yf_acceptEventInterval`  时间段后才可以再次响应点击事件
@property (nonatomic, assign) NSTimeInterval yf_acceptEventInterval;
@end

.m

#import "UIControl+YFAdd.h"

@interface UIControl ()
@property (nonatomic, assign) NSTimeInterval yf_acceptEventTime; // 接收事件的时间
@end

@implementation UIControl (YFAdd)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ReplaceMethod([self class], @selector(pointInside:withEvent:), @selector(yf_pointInside:withEvent:));
        ReplaceMethod([self class], @selector(sendAction:to:forEvent:), @selector(yf_sendAction:to:forEvent:));
    });
}
/// 捕获 `pointInside` 方法
- (BOOL)yf_pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (UIEdgeInsetsEqualToEdgeInsets(self.yf_touchExtendInset, UIEdgeInsetsZero) || self.isHidden ||
        ([self isKindOfClass:UIControl.class] && !((UIControl *)self).isEnabled)) {
        return [self yf_pointInside:point withEvent:event];
    }
    CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, self.yf_touchExtendInset);
    hitFrame.size.width = MAX(hitFrame.size.width, 0);
    hitFrame.size.height = MAX(hitFrame.size.height, 0);
    return CGRectContainsPoint(hitFrame, point);
}

static char * kAssociated_touchExtendInset = "kAssociatedObject_touchExtendInset";
- (void)setYf_touchExtendInset:(UIEdgeInsets)yf_touchExtendInset {
    objc_setAssociatedObject(self, &kAssociated_touchExtendInset, [NSValue valueWithUIEdgeInsets:yf_touchExtendInset],
                             OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIEdgeInsets)yf_touchExtendInset {
    return [objc_getAssociatedObject(self, &kAssociated_touchExtendInset) UIEdgeInsetsValue];
}

// 关联
static const char * kAssociated_acceptEventInterval = "kAssociated_acceptEventInterval";

- (NSTimeInterval)yf_acceptEventInterval {
    return  [objc_getAssociatedObject(self, kAssociated_acceptEventInterval) doubleValue];
}

- (void)setYf_acceptEventInterval:(NSTimeInterval)yf_acceptEventInterval {
    objc_setAssociatedObject(self, kAssociated_acceptEventInterval, @(yf_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static const char * kAssociated_acceptEventTime = "kAssociated_acceptEventTime";
- (NSTimeInterval)yf_acceptEventTime {
    return  [objc_getAssociatedObject(self, kAssociated_acceptEventTime) doubleValue];
}

- (void)setYf_acceptEventTime:(NSTimeInterval)yf_acceptEventTime {
    objc_setAssociatedObject(self, kAssociated_acceptEventTime, @(yf_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)yf_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    NSTimeInterval nowTime = [NSDate date].timeIntervalSince1970;
    NSTimeInterval acceptEventTime = self.yf_acceptEventTime;
    NSTimeInterval acceptEventInterval = self.yf_acceptEventInterval;
    if (nowTime - acceptEventTime < acceptEventInterval) return;
    if (self.yf_acceptEventInterval > 0) {
        self.yf_acceptEventTime = [NSDate date].timeIntervalSince1970;
    }
    [self yf_sendAction:action to:target forEvent:event];
}


  • 交换方法

/** 交换方法 */

CG_INLINE void
ReplaceMethod(Class _class, SEL _originSelector, SEL _newSelector) {
    Method oriMethod = class_getInstanceMethod(_class, _originSelector);
    Method newMethod = class_getInstanceMethod(_class, _newSelector);
    BOOL isAddedMethod = class_addMethod(_class, _originSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (isAddedMethod) {
        class_replaceMethod(_class, _newSelector, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
    } else {
        method_exchangeImplementations(oriMethod, newMethod);
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 58,406评论 51 604
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,249评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,115评论 19 139
  • 在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:L...
    WQ_UESTC阅读 6,154评论 4 26
  • 你在闹,我在笑,初来乍到
    看书挺好的阅读 281评论 0 0