UIControl:touchdragenter 事件
touchdragenter 事件在手指按住、从控件外移回控件内,越过边界的那一刻派发。
语法
addEventListener('touchdragenter', (event) => { })事件对象
事件对象的类型是 UIControlTouchEvent,除了 type 和 target,还带两个只读字段:
| 字段 | 类型 | 含义 |
|---|---|---|
location | CGPoint 或 null | 手指的位置,在这个控件自己的坐标系里,单位 pt。由 sendActionsForControlEvents() 派发时是 null |
tapCount | 数 | 连击次数,单位是次。由 sendActionsForControlEvents() 派发时是 0 |
回调参数的类型声明是 Event,读这两个字段要转成 UIControlTouchEvent,见示例。
不能取消:cancelable 是 false,preventDefault() 没有效果。
什么时候派发
- 每次越过边界派发一次。未在模拟器验证。
- 所有控件都派发。
- 之前一定派发过
touchdragexit:手指要先移出去才能移回来。
一次触摸的事件序列
普通点击(按下,不移动,松开),在直接创建的 UIControl 上实测:
touchdown → touchupinside按下后滑出去再松手,前四步实测,最后一步 touchupoutside 未在模拟器验证:
touchdown
→ touchdraginside(手指在内部每移动一次派发一次)
→ touchdragexit(一次)
→ touchdragoutside(手指在外部每移动一次派发一次)
→ touchupoutside滑出去又滑回来再松手:在 touchdragoutside 之后是 touchdragenter、touchdraginside,最后是 touchupinside。未在模拟器验证。
示例
js
const card = new UIControl();
card.addEventListener('touchdragexit', () => {
card.alpha = 1;
});
card.addEventListener('touchdragenter', () => {
card.alpha = 0.6;
});效果:手指按住 card 滑出去,恢复不透明;不松开滑回来,变淡。未在模拟器验证。
对应的 iOS API
UIControlEventTouchDragEnter。原生用 addTarget:action:forControlEvents: 登记,这里用 addEventListener()。
参见
touchdragexit:移到控件外- 触点位置
- 回调的执行时机