iOS上的圆形按钮

glo*_*ser 4 iphone geometry objective-c button ios

是否可以创建如下图所示的两个按钮?看起来你应该使用UIButton或UIImageView但是如果我点击区域1,它仍然可以点击按钮1.当我点击区域1时,按钮2应该被触发!

在此输入图像描述

Tom*_*mmy 7

如果上述响应未被接受,您可以实现UIButton该覆盖的自定义子类pointInside:withEvent:.

假设您的视图完全是正方形,图形完全是圆形并填充整个正方形,示例可能是:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    // check that the point is at least inside the normal rect
    if(![super pointInside:point withEvent:event]) return NO;

    // we'll assume a square view with an exact circle inside, so
    // the radius of the circle is just half the width
    CGFloat radius = self.bounds.size.width*0.5f;

    // the point (radius, radius) is also the centre of the view, so...
    CGFloat squareOfDistanceFromCentre =
          (radius - point.x)*(radius - point.x) +
          (radius - point.y)*(radius - point.y);

    // if the point is too far away, return NO
    if(squareOfDistanceFromCentre > radius*radius) return NO;

    // otherwise we've exhausted possible reasons for answering NO
    return YES;
}
Run Code Online (Sandbox Code Playgroud)