UIView局部点击
2014-03-03 19:11
302 查看
今天上班遇到一种情况,需要局部响应点击事件,比如在一个UIImageView中设置一个小圆圈图片,要求点击圆圈里面不响应点击,点击小圆圈外面的部分响应点击。
可以通过重写hitTest:withEvent: 和 pointInside: withEvent:方法来做到。
看一下hitTest:withEvent
*touch事件发生,创建UIEvent对象
*按照Application的view层次结构,逐层调用每个view的hitTest:withEvent:方法,并传入该event对象,view根据hitTest:withEvent:方法和来决定touch点是否包含在自己的bounds中;
*如果view的bounds包含了touch点,该view会遍历自己的subview,并调用每个subview的pointInside:withEvent:方法来进一步决定touch事件是发生在自己本身,还是自己的subview上。
*重复第二,三步,并筛选出最终接受touch事件的view对象
//继承 建子类 重写上述方法
View Code
//使用部分的代码
//中间的图片
//这样就达到了点击局部响应事件的效果。关于userInteractionEnabled这个属性设置为NO的时候,表示这个view从事件队列移除出去。
可以通过重写hitTest:withEvent: 和 pointInside: withEvent:方法来做到。
看一下hitTest:withEvent
*touch事件发生,创建UIEvent对象
*按照Application的view层次结构,逐层调用每个view的hitTest:withEvent:方法,并传入该event对象,view根据hitTest:withEvent:方法和来决定touch点是否包含在自己的bounds中;
*如果view的bounds包含了touch点,该view会遍历自己的subview,并调用每个subview的pointInside:withEvent:方法来进一步决定touch事件是发生在自己本身,还是自己的subview上。
*重复第二,三步,并筛选出最终接受touch事件的view对象
//继承 建子类 重写上述方法
@interface CustomImageView : UIImageView @property (strong, nonatomic) UIBezierPath *bezierPath; @end @implementation CustomImageView - (id)init { self = [super init]; if (self) { } return self; } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *result = [super hitTest:point withEvent:event]; return [self pointInside:point withEvent:event] ? self : result; } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { return CGPathContainsPoint(self.bezierPath.CGPath, NULL, point, YES) ? YES : NO; } @end
View Code
//使用部分的代码
//中间的图片
CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0,0, imageWidth02, imageHeight02)]; imageView.backgroundColor = [UIColor clearColor]; imageView.userInteractionEnabled = YES; imageView.bezierPath = [UIBezierPath bezierPathWithOvalInRect:imageView.bounds]; imageView.image = [UIImage imageNamed:kCenterImg]; [self.view addSubview:imageView];
//这样就达到了点击局部响应事件的效果。关于userInteractionEnabled这个属性设置为NO的时候,表示这个view从事件队列移除出去。
相关文章推荐
- ios UITableView
- new (addr)type(value)
- Java多线程-工具篇-BlockingQueue
- 查看指定路径下的文件名,并使用UUID生成新的文件名
- iOS开发:UIScrollView如何禁止上下滑动
- Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现
- ZOJ-3349 Special Subsequence 线段树优化DP
- Windows phone UI虚拟化和数据虚拟化(二)
- 从零开始学ios开发(十二):Table Views(中)UITableViewCell定制
- 1388[quicksum]
- Windows phone UI虚拟化和数据虚拟化(一)
- Select标签出现了The requested list key 'noticeType' could not be resolved as a collection/array/map/enume
- 移动开发:Android 4.2 InCallScreen UI浅析
- 原来可以找到没混淆的easyui
- ios中UI和常用动画汇总 抖动等现成的动画
- ios 调整UILabel的行高
- IOS7学习之路九(ios7自定义UIAlertView)
- IOS7学习之路九(ios7自定义UIAlertView)
- [代码分享]调整UILabel行高间距(利用iOS 6的特性来手动调整行间矩)
- libgdx——常见UI的使用