UIButton的“触摸拖动/退出”点击区域的大小如何? [英] What's up with the size of UIButton's Touch Drag/Exit hit area?
问题描述
好吧,我想最好是显示我的意思:
Well, I guess it is best to show what I mean:
您可以清楚地看到,一旦我们触摸了按钮并将其移出,
You can clearly see that once we've touched the button and moved out of it, a consequent move-in event triggers the button state change from far away.
虽然所有UIButton都具有这种行为,但我无法通过Google解决方案对其进行更改。
While this behavior is natural for all UIButtons, I couldn't google a solution to alter it.
p>
有没有办法减少这种类型的UIButton敏感度的点击区域?我希望将其缩小,因为我觉得按钮足够大了,可以提供更好的用户体验以及上/下声音效果。
Is there a way to reduce the hit area for this type of UIButton sensitivity? I want it reduced, because I feel that the button is large enough as it is, and it will provide a better user experience along with up/down sound effects.
UPD:以下UIButton的替代代码发布在另一个线程:
UPD: The following override code for UIButton was posted in another thread:
- (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];
}
它更改了Drag In / Drag Out事件使用的点击区域扩展按钮的上/下状态切换与以前完全相同。
It alters the hit area extension used by Drag In/Drag Out events, yet button Up/Down states switch exactly the same way as they did before.
推荐答案
我不知道您是否仍在使用相同的问题,但是我能够通过在touchesEnded:withEvent:方法中使用类似的代码来修复它。
I don't know if you are still having the same issue, but I was able to fix it by using similar code in the touchesEnded:withEvent: method.
我还更改了该方法以添加touchEnter和dragInside,因为在当前代码中,那些事件仍然使用相同的界限。另外,我使每种情况都返回YES,以便不调用super(这会导致过早调用内部的触摸拖动)。
I also changed that method to add touchEnter and dragInside because with the current code, those to events still used the same bounds. In addition I made each of the cases return YES so that the super is not called (it would cause the touch drag inside to be called prematurely).
这是最后一个我最终得到的代码有两种方法:
Here is the final code that I ended up with, in two methods:
- (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) {
[self sendActionsForControlEvents:UIControlEventTouchDragExit];
return YES;
}
else
{
[self sendActionsForControlEvents:UIControlEventTouchDragOutside];
return YES;
}
}
else {
BOOL previousTouchOutside = !CGRectContainsPoint(outerBounds, [touch previousLocationInView:self]);
if (previousTouchOutside) {
[self sendActionsForControlEvents:UIControlEventTouchDragEnter];
return YES;
}
else {
[self sendActionsForControlEvents:UIControlEventTouchDragInside];
return YES;
}
}
return [super continueTrackingWithTouch:touch withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGFloat boundsExtension = 25.0f;
CGRect outerBounds = CGRectInset(self.bounds, -1 * boundsExtension, -1 * boundsExtension);
BOOL touchInside = CGRectContainsPoint(outerBounds, [touch locationInView:self]);
if (touchInside) {
return [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else {
return [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
}
return [super endTrackingWithTouch:touch withEvent:event];
}
注意:不必在最后返回方法的上级,但是为了完整起见,我把它留在那里。
NOTE: Returning the super of the method at the end is not necessary, but I left it in there for completeness.
这篇关于UIButton的“触摸拖动/退出”点击区域的大小如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!