美文网首页iOS开发专题iOS 知识点
iOS 扩大 View 的点击区域, 设置点击时间间隔

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

作者: 阿拉灯神钉 | 来源:发表于2017-09-18 15:22 被阅读37次
    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);
        }
    }
    
    

    相关文章

      网友评论

        本文标题:iOS 扩大 View 的点击区域, 设置点击时间间隔

        本文链接:https://www.haomeiwen.com/subject/oerbsxtx.html