我敢肯定这听起来像是一个菜鸟问题,但不知何故我卡住了,我在这里需要帮助。考虑我的 uibutton,它的背景图像有一个随意的可见边界,当点击时触摸按钮边界内,而不是专门在随意可见的边界,这完全有道理。但是我想将其触摸限制在可见边界而不是整个按钮。请在附件中找到解释我想要触摸的地方(绿色勾选)和不喜欢的地方(红叉)的图像。在此先感谢您。
最佳答案
继承 UIButton 并实现 pointInside:withEvent
方法。 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/pointInside:withEvent :
当系统想知道为事件指定的点是否在您的视野内时,将调用此方法。如果是,则简单地返回 YES,如果不是,则返回 NO。
您所需要的只是一种确定该点是否在您的 UIButton 单击区域中的方法。当返回 YES 时。
像下面这样使用 CGRects 数组的东西:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
// Only pass through of the point is in a specific area
BOOL ret=NO;
for (NSValue *value in self.passThroughAreas){
CGRect rect=value.CGRectValue;
if (CGRectContainsPoint(rect, point)){
// Its in one of our pass through areas. Let the events pass.
ret=YES;
break;
}
}
return ret;
}
您使用的矩形越多,它们越小,您的区域就越有针对性。如果您最终要使用太多,也可以使用更高级的东西。
关于ios - 将 UIButton 的触摸限制在指定区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32436954/