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
}
}
|