是否可以创建两个如下图所示的按钮?看起来您应该使用 UIButton 或 UIImageView,但如果我单击到区域 1,它仍然像单击按钮 1 一样。当我单击到区域 1 时,按钮 2 也应该被触发!
最佳答案
如果上述响应 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/