Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在自定义UIControl对象中定义自定义触摸区域_Ios_Uicontrol - Fatal编程技术网

Ios 在自定义UIControl对象中定义自定义触摸区域

Ios 在自定义UIControl对象中定义自定义触摸区域,ios,uicontrol,Ios,Uicontrol,我正在创建一个自定义的UIControl对象。除了触摸区外,其他功能都很好 我想找到一种方法,将触摸区域限制为控件的一部分,在上面的示例中,我希望它仅限于黑色圆周,而不是整个控件区域 有什么想法吗? 干杯您可以覆盖UIView以拒绝不需要的触摸 以下是一种检查触摸是否发生在视图中心周围的圆环中的方法: - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { UITouch *touch = [[event touch

我正在创建一个自定义的UIControl对象。除了触摸区外,其他功能都很好

我想找到一种方法,将触摸区域限制为控件的一部分,在上面的示例中,我希望它仅限于黑色圆周,而不是整个控件区域

有什么想法吗? 干杯

您可以覆盖UIView以拒绝不需要的触摸

以下是一种检查触摸是否发生在视图中心周围的圆环中的方法:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
    if (touch == nil)
        return NO;

    CGPoint touchPoint = [touch locationInView:self];
    CGRect bounds = self.bounds;

    CGPoint center = { CGRectGetMidX(bounds), CGRectGetMidY(bounds) };
    CGVector delta = { touchPoint.x - center.x, touchPoint.y - center.y };
    CGFloat squareDistance = delta.dx * delta.dx + delta.dy * delta.dy;

    CGFloat outerRadius = bounds.size.width * 0.5;

    if (squareDistance > outerRadius * outerRadius)
        return NO;

    CGFloat innerRadius = outerRadius * 0.5;

    if (squareDistance < innerRadius * innerRadius)
        return NO;

    return YES;
}
-(BOOL)点内部:(CGPoint)点与事件:(UIEvent*)事件
{
UITouch*touch=[[event touchesForView:self]anyObject];
如果(触摸==零)
返回否;
CGPoint接触点=[touch locationInView:self];
CGRect边界=自边界;
CGPoint center={CGRectGetMidX(边界),CGRectGetMidY(边界)};
CGVector delta={touchPoint.x-center.x,touchPoint.y-center.y};
CGFloat squaredance=delta.dx*delta.dx+delta.dy*delta.dy;
CGFloat outerRadius=bounds.size.width*0.5;
if(平方距离>外半径*外半径)
返回否;
CGFloat内半径=外半径*0.5;
if(平方距离<内半径*内半径)
返回否;
返回YES;
}

要检测更复杂形状上的其他点击,您可以使用
CGPath
来描述形状,并使用
CGPathContainsPoint
进行测试。另一种方法是使用控件的图像并测试像素的alpha值


所有这一切都取决于您如何构建控件。

(我们不是来做所有工作的-您至少需要在阅读相关文档后亲自尝试解决此问题。)我发现这可能会完成您感兴趣的工作。我知道这一点,但如何检测使用核心图形绘制的特定形状内的触摸。再次从我提供的示例来看,如何检测在UICONTL的drawRect中绘制的黑色圆弧内的交点?好的,谢谢您的回答。这更像是一项工作,我非常感谢。这是一个很好的解决办法。但我想知道是否有其他方法检测与UIControl内容的交互。在这个例子中,区域很简单,只是一个粗弧。但我使用不同多边形形状的区域。“有没有一种直接的方法来检测这些呢?”Abolfoud编辑回答。“另一种方法是使用控件的图像并测试像素的alpha值。”这听起来是个好主意。我要试一试。谢谢