iphone - iOS 上的圆形按钮

标签 iphone objective-c ios button geometry

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

enter image description here

最佳答案

如果上述响应 Not Acceptable ,您可以实现 UIButton 的自定义子类来覆盖 pointInside:withEvent: .

假设您的 View 完全是正方形,图形完全是圆形并填满整个正方形,例如:

- (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;
}

关于iphone - iOS 上的圆形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871740/

相关文章:

iphone - 尝试以编程方式定位按钮,但根据 X、Y 不在正确位置

ios - 如何弹出 UIImagePickerController 的导航

objective-c - APNs 提供的 Device Token 的格式在我的应用程序中突然改变了吗?

iphone - Obj-C,货币值(value)与可用数量,发行

ios - 通过 SOTI Mobicontrol 分发 iOS 应用程序需要什么

html - 如何在 iOS 中共享时将可点击的链接发送到 Gmail 邮件正文和 google plus 邮件

iphone - 将连接更改为 3G 或 EDGE 以测试慢速互联网连接

ios - UIView.animate 未在 PHPhotoLibrary.shared().performChanges 中执行

ios - 通过带有参数的 Siri 启动应用程序,无需捐赠/快捷方式

iphone - 如何降低UIScrollview的滚动速度