iOS点击事件穿透及扩大视图点击区域

前言

点击事件穿透指的是点击当前视图,但是实际上被选中的是其他视图。举个例子,下方效果图中有两个按钮,当点击不重合的地方,显示的是点击当前视图,当点击重合地方时,点击的是下方的视图。


点击事件扩大区域指的是点击当前视图区域外的位置,仍然显示的是点击当前视图。举个例子,下方效果图中黄色部分是按钮,红色部分是按钮外的区域,但是点击红色部分,仍能显示点击黄色按钮。

说到这里,应该对这两种效果有一个初步的理解了。那这两种效果是怎么实现的呢?这两种效果是基于系统的SDK- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event实现的。这个方法是UIView的一个实例方法,官方的注释如下:

recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system

意思是这个方法会递归调用-pointInside:withEvent:这个方法,point是接收者的坐标系,也就是点击的这个点在视图中的相对坐标。

点击事件穿透

实现点击事件穿透的本质是在视图中判断点击的点是否属于你想要点击的视图,然后判断返回相应的视图即可。
在这里,附上上方效果做法。
自定义按钮视图,并放置一个可传入的按钮。如下:

#import <UIKit/UIKit.h>

@interface RedButton : UIButton
@property (strong, nonatomic) UIButton *button;

@end

#import "RedButton.h"

@interface RedButton()

@end

@implementation RedButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor redColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = frame.size.width / 2;
    }
    return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint redPoint = [self convertPoint:point toView:self.button];
    if ([self.button pointInside:redPoint withEvent:event]) {
        return self.button;
    }else {
        return [super hitTest:point withEvent:event];
    }
}
@end

设置UI和点击方法,如下:

#pragma mark 点击事件穿透
- (void)setUpSubViewToThrough {
    
    CGFloat blue_width = screen_width / 3;
    CGFloat blue_height = blue_width;
    CGFloat blue_x = (screen_width - blue_width * 2) / 2;
    CGFloat blue_y = (screen_height - blue_height * 2) / 2;
    UIButton *blue = [[UIButton alloc]init];
    [blue setFrame:(CGRect){blue_x, blue_y, blue_width, blue_height}];
    [blue setBackgroundColor:[UIColor blueColor]];
    [blue addTarget:self action:@selector(blueButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:blue];
    
    CGFloat red_width = screen_width / 2;
    CGFloat red_height = red_width;
    CGFloat red_x = blue_x * 2;
    CGFloat red_y = (screen_height - blue_height) / 2;
    RedButton *red = [[RedButton alloc]initWithFrame:(CGRect){red_x, red_y, red_width, red_height}];
    [red addTarget:self action:@selector(redButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:red];
    
    red.button = blue;
}

- (void)blueButtonAction:(UIButton *)sender {
    NSLog(@"点击蓝按钮");
}

- (void)redButtonAction:(UIButton *)sender {
    NSLog(@"点击红按钮");
}

扩大视图点击区域

扩大视图点击区域的本质是判断所点击的点是否在于你想要扩大的区域上。在这里,楼主所设置的扩大区域是边线外增加30。实现如下,自定义视图:

#import <UIKit/UIKit.h>

@interface YellowButton : UIButton

@end

#import "YellowButton.h"

@interface YellowButton()

@end

@implementation YellowButton

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor yellowColor];
    }
    return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGFloat min_x = -30.0;
    CGFloat min_y = -30.0;
    CGFloat max_x = self.frame.size.width + 30;
    CGFloat max_y = self.frame.size.height + 30;
    
    if (point.x <= max_x && point.x >= min_x && point.y <= max_y && point.y >= min_y) {
        point = CGPointMake(0, 0);
        return [super hitTest:point withEvent:event];
    }else {
        return [super hitTest:point withEvent:event];
    }
    
}
@end

设置UI和点击方法,如下:

#pragma mark 扩大点击区域
- (void)setUpSubViewToArea {
    
    CGFloat yellow_width = screen_width / 3;
    CGFloat yellow_height = yellow_width;
    CGFloat yellow_x = (screen_width - yellow_width) / 2;
    CGFloat yellow_y = (screen_height - yellow_height) / 2;
    YellowButton *yellow = [[YellowButton alloc]initWithFrame:(CGRect){yellow_x, yellow_y, yellow_width, yellow_height}];
    [yellow addTarget:self action:@selector(yellowButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:yellow];
    
    CGFloat backgroundView_width = yellow_width + 60;
    CGFloat backgroundView_height = backgroundView_width;
    CGFloat backgroundView_x = yellow_x - 30;
    CGFloat backgroundView_y = yellow_y - 30;
    UIView *backgroundView = [[UIView alloc]initWithFrame:(CGRect){backgroundView_x, backgroundView_y, backgroundView_width, backgroundView_height}];
    backgroundView.backgroundColor = [UIColor redColor];
    [self.view insertSubview:backgroundView belowSubview:yellow];
}

- (void)yellowButtonAction:(UIButton *)sender {
    NSLog(@"点击黄按钮");
}

希望这篇文章对各位看官有所帮助,Demo下载地址:Demo,对支持小编的看官们表示感谢。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • 重点参考链接: View Programming Guide for iOS https://developer....
    Kevin_Junbaozi阅读 4,377评论 0 15
  • 问题 最近要在iPad上实现一个很独特的功能,简单描述一下就是要显示一个带有半透明背景的弹出界面,在其上加一个不规...
    从小荔枝炸清华阅读 3,996评论 2 3
  • 在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:L...
    WQ_UESTC阅读 5,980评论 4 26
  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 56,219评论 51 598
  • 下午抽时间想好了主题,关于生活,关于迷茫,谁的生活不迷茫,动笔写了一千多字,快完结了,赶着上地铁去上书法课...
    闲度阅读 377评论 0 0