星空网 > 软件开发 > 操作系统

2.0 触摸事件

UIView不接收触摸事件的三种情况:

 

1、不接收用户交互
userInteractionEnabled = NO
2、隐藏
hidden = YES
3、透明
alpha = 0.0 ~ 0.01
4. 如果子视图的位置超出了父视图的有效范围, 那么子视图也是无法与用户交互的, 即使设置了父视图的 clipsToBounds = NO, 可以看懂, 但是也是无法与用户交互的
 

提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的
 
 
触摸事件中的方法:


 
 1 // 手指按下的时候调用 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event; 3  4 // 手指移动的时候调用 5 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event; 6  7 // 手指抬起的时候调用 8 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event; 9 10 // 取消(非正常离开屏幕,意外中断事件)11 - (void)touchesCancelled:(nullableNSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event;

目前还没有找到相关使用方法,如果有知道的朋友请联系我,非常感激。。。

1 // 3D Touch相关方法,当前触摸对象估计的触摸特性,返回值是UITouchPropertyie2 - (void)touchesEstimatedPropertiesUpdated:(NSSet * _Nonnull)touches NS_AVAILABLE_IOS(9_1);

相关代码:

 1 #import “TDView.h" 2  3 @implementation TDView 4  5 // 手指触摸到这个view的时候调用 6 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 7  8  UITouch *t = touches.anyObject; 9 10  NSLog(@"%ld", t.tapCount); // 点击这个响应者对象的次数11 12  NSLog(@"%@", t.window); // 点击这个响应者对象所在的window13  NSLog(@"%@", self.window);14  NSLog(@"%@", [UIApplication sharedApplication].keyWindow);15 16  NSLog(@"%s", __func__);17 }18 19 // 手指在这个view上移动的时候调用20 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {21  // 获取触摸对象22  UITouch *t = touches.anyObject;23 24  // 获取上一个点的位置25  CGPoint lastP = [t previousLocationInView:self.superview];26  NSLog(@"%@----上一个点的位置", NSStringFromCGPoint(lastP));27 28  // 获取当前点的位置29  CGPoint p = [t locationInView:self.superview];30  NSLog(@"%@----当前点的位置", NSStringFromCGPoint(p));31 }32 33 // 手指离开这个view的时候调用34 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {35    NSLog(@"%s", __func__);36 }37 38 // 取消(非正常离开屏幕)39 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {40    NSLog(@"%s", __func__);41 }42 43 @end

 


补充:关于NSSet和 NSArray

 
 
如何访问
如何取值
如何遍历
效率
                   应用场景
 
 
NSSet
集合
无序访问,不重复的。
 
存放到 NSSet 中的内容并不会排序与添加顺序也没有关系
取集合里面任意一个元素 anyObject
 
通过 anyObject 来访问单个元素
for in 效率高(1)比如重用 Cell 的时候, 从缓存池中随便获取一个就可以了, 无需按照指定顺序来获取
 
(2)当需要把数据存放到一个集合中, 然后判断集合中是否有某个对象的时候
 
 
NSArray
数组
有序访问,可以有重复对象。
 
对象的顺序是按照添加的顺序来保存的
 
通过下标来访问
for 
 
forin
  
当需要把数据存放到一个集合中, 然后判断集合中是否有某个对象的时候

 
 



如有疑问,请发送邮件至 shorfng@126.com 联系我。
 
By:蓝田(Loto)

 



原标题:2.0 触摸事件

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Bloomingdale's 布鲁明黛 :https://www.goluckyvip.com/tag/16532.html
bloomingltd:https://www.goluckyvip.com/tag/16533.html
Bloomscape:https://www.goluckyvip.com/tag/16534.html
BLS Int'l:https://www.goluckyvip.com/tag/16535.html
Blue Apron:https://www.goluckyvip.com/tag/16536.html
Blue Nile:https://www.goluckyvip.com/tag/16538.html
TikTok斥资210万美元游说美国参议院阻止法案通过 :https://www.goluckyvip.com/news/188220.html
北京飞机票查询(快速查询北京至各地机票价格和航班信息):https://www.vstour.cn/a/366178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流