iOS开发之手势gesture详解(二)
与其他用户界面控件交互
UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用于手势识别重叠的默认动作的控制,其中包括:
- 一根手指单击动作:UIButton, UISwitch, UIStepper, UISegmentedControl, and UIPageControl.
- 一根手指擦碰动作:UISlider
- 一根手指拖动动作:UISwitch
包含多点触摸的事件
在iOS中,touch是指一根手指在屏幕上滑动、移动.而手势是指有一根或多根手指,它的类是UITouch对象.多点触摸事件是UIEventTypeTouches
当发生触摸时,会触发下列方法:
- touchesBegan:withEvent :触摸屏幕时发生
- touchesMoved:withEvent :手指在屏幕上移动时发生
- touchesEnded:withEvent :触摸结束时发生,即手指离开屏幕时
- touchesCancelled:withEvent :触摸被系统取消时发生,如有电话时,该触摸会被取消
改变默认的事件传递顺序
你可以通过改变UIGestureRecognizer子类的属性来改变默认的事件传递
- @property(nonatomic) BOOL delaysTouchesBegan; // default is NO. causes all touch events to be delivered to the target view only after this gesture has failed recognition. set to YES to prevent views from processing any touches that may be recognized as part of this gesture
- @property(nonatomic) BOOL delaysTouchesEnded; // default is YES. causes touchesEnded events to be delivered to the target view only after this gesture has failed recognition. this ensures that a touch that is part of the gesture can be cancelled if the gesture is recognized
如果一个gesture recognizer检测到了一个不属于它的touch,它将手势直接发送给它的view,此时会调用ignoreTouch:forEvent: 方法直接传递给view.
创建自定义gesture recognizer步骤
- 创建UIGestureRecognizer的子类
- #import <UIKit/UIGestureRecognizerSubclass.h>
- 在子类冲重载下列方法
1.- (void)reset;
2.- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
3.- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
4.- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
5.- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架