跳出手掌心--如何立即触发UIButton边界事件
来源:互联网 发布:素描相机软件下载 编辑:程序博客网 时间:2024/06/13 23:40
1
An event where a finger is dragged from within a control to outside its bounds.
来自StackOverflow的答案
检验结果
换个思路
注册回调
回调函数
处理TouchUp事件
结尾
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
1
2
3
4
5
6
7
8
9
10
11
12
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
/*
Description
Sent continuously to the control as it tracks a touch related to the given event within the control’s bounds.
Parameters
touch
A UITouch object that represents a touch on the receiving control during tracking.
event
An event object encapsulating the information specific to the user event
Returns
YES if touch tracking should continue; otherwise NO.
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
CGFloat boundsExtension = 25.0f;
CGRect outerBounds = CGRectInset(self.bounds, -1 * boundsExtension, -1 * boundsExtension);
BOOL touchOutside = !CGRectContainsPoint(outerBounds, [touch locationInView:self]);
if
(touchOutside) {
BOOL previousTouchInside = CGRectContainsPoint(outerBounds, [touch previousLocationInView:self]);
if
(previousTouchInside) {
NSLog(@
"Sending UIControlEventTouchDragExit"
);
[self sendActionsForControlEvents:UIControlEventTouchDragExit];
}
else
{
NSLog(@
"Sending UIControlEventTouchDragOutside"
);
[self sendActionsForControlEvents:UIControlEventTouchDragOutside];
}
}
return
[
super
continueTrackingWithTouch:touch withEvent:event];
}
UIControlEventTouchDragExit会响应两次,分别为:
手指离开button边界25个像素时触发
第二次依然是70个像素时触发,这是UIButton的默认行为
第二个问题是在事件的回调函数:
- (void)callback:(UIButton *)sender withEvent:(UIEvent *)event
中,由UIEvent参数计算得到的位置始终是(0, 0),它并未正确的初始化
1
1
return
[
super
continueTrackingWithTouch:touch withEvent:event];
1
2
3
// to get the drag event
[btn addTarget:self action:@selector(btnDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[btn addTarget:self action:@selector(btnDragged:withEvent:) forControlEvents:UIControlEventTouchDragOutside];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (void)btnDragged:(UIButton *)sender withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGFloat boundsExtension = 25.0f;
CGRect outerBounds = CGRectInset(sender.bounds, -1 * boundsExtension, -1 * boundsExtension);
BOOL touchOutside = !CGRectContainsPoint(outerBounds, [touch locationInView:sender]);
if
(touchOutside) {
BOOL previewTouchInside = CGRectContainsPoint(outerBounds, [touch previousLocationInView:sender]);
if
(previewTouchInside) {
// UIControlEventTouchDragExit
}
else
{
// UIControlEventTouchDragOutside
}
}
else
{
BOOL previewTouchOutside = !CGRectContainsPoint(outerBounds, [touch previousLocationInView:sender]);
if
(previewTouchOutside) {
// UIControlEventTouchDragEnter
}
else
{
// UIControlEventTouchDragInside
}
}
}
1
2
3
// to get the touch up event
[btn addTarget:self action:@selector(btnTouchUp:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(btnTouchUp:withEvent:) forControlEvents:UIControlEventTouchUpOutside];
1
2
3
4
5
6
7
8
9
10
11
- (void)btnTouchUp:(UIButton *)sender withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGFloat boundsExtension = 25.0f;
CGRect outerBounds = CGRectInset(sender.bounds, -1 * boundsExtension, -1 * boundsExtension);
BOOL touchOutside = !CGRectContainsPoint(outerBounds, [touch locationInView:sender]);
if
(touchOutside) {
// UIControlEventTouchUpOutside
}
else
{
// UIControlEventTouchUpInside
}
}
0 0
- 跳出手掌心——如何立即触发UIButton边界事件
- 跳出手掌心--如何立即触发UIButton边界事件
- 如何立即触发UIButton边界事件
- UIButton与其触发事件全
- 触发UIButton长按事件
- 触发UIButton对应的事件
- 触发UIButton长按事件
- 文本框输入后立即触发事件
- UIButton事件防止多次触发的写法
- ios 触发UIButton长按事件
- 当UIButton触发点击事件时获取触发点坐标
- 移动端touchmove事件边界touchend不触发
- UIButton 触发多个事件 响应同一函数
- 在ViewDidLoad 里添加UIButton的点击事件,不能触发。
- TextBox如何触发TextChanged事件
- UITabBarItem 如何触发点击事件
- UITabBarItem 如何触发点击事件
- 如何UIButton添加事件并传递参数
- App自动化测试工具总结
- J2EE总结------JMS消息服务
- ubuntu 12.04 hadoop 单机模式平台搭建
- 心情随笔(二):坚持就是胜利!
- yuv和ycrcb的差异
- 跳出手掌心--如何立即触发UIButton边界事件
- 监听器 listener 例子
- JSON 之GSON 解析
- dubbo注册zookepper神奇IP_续
- pay---Android 银行卡快捷支付
- 编译型语言VS解释型语言、动态语言、静态语言
- Java中String、StringBuffer和StringBuilder的区别和堆栈内存分配
- IAP相关资源收集及开发总结,帮助你快速实现IAP功能
- 又一次重新的开始!