UIControlEventTouchDragInside當(dāng)一次觸摸在控件窗口內(nèi)拖動(dòng)時(shí)轻局。UIControlEventTouchDragOutside當(dāng)一次觸摸在控件窗口之外拖動(dòng)時(shí)侍匙。UIControlEventTouchDragEnter當(dāng)一次觸摸從控件窗口之外拖動(dòng)到內(nèi)部時(shí)迈喉。UIControlEventTouchDragExit當(dāng)一次觸摸從控件窗口內(nèi)部拖動(dòng)到外部時(shí)集歇。
[btn addTarget:self action:@selector(sendButtonDrag:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[btn addTarget:self action:@selector(sendButtonDrag:withEvent:) forControlEvents:UIControlEventTouchDragOutside];
(IBAction)sendButtonDrag:(UIButton *)sender withEvent:(UIEvent *)event {
if ([self isInButtonBounds:sender event:event]) {
// 在里面
} else {
// 在外面
}
}(BOOL)isInButtonBounds:(UIButton *)button event:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGFloat boundsExtension = 5.0f; //擴(kuò)展范圍閥值
CGRect outerBounds = CGRectInset(button.bounds, -1 * boundsExtension, -1 * boundsExtension);
BOOL touchOutside = !CGRectContainsPoint(outerBounds, [touch locationInView:button]);
if (touchOutside) {
BOOL previewTouchInside = CGRectContainsPoint(outerBounds, [touch previousLocationInView:button]);
if (previewTouchInside) {
// UIControlEventTouchDragExit
} else {
// UIControlEventTouchDragOutside
}
return NO;
} else {
BOOL previewTouchOutside = !CGRectContainsPoint(outerBounds, [touch previousLocationInView:button]);
if (previewTouchOutside) {
// UIControlEventTouchDragEnter
} else {
// UIControlEventTouchDragInside
}
return YES;
}
}
- (IBAction)sendAudioRecordButton:(id)sender withEvent:(UIEvent *)event {
if ([self isInButtonBounds:sender event:event]) {
// 點(diǎn)擊按鈕
}
}