是否可以创建如下图所示的两个按钮?看起来似乎应该使用UIButton或UIImageView,但如果我点击区域1,它仍然会像点击按钮1一样操作。当我点击区域1时,也应该触发按钮2!
是否可以创建如下图所示的两个按钮?看起来似乎应该使用UIButton或UIImageView,但如果我点击区域1,它仍然会像点击按钮1一样操作。当我点击区域1时,也应该触发按钮2!
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;
}
通过裁剪图层并设置按钮的半径,您可以制作圆形按钮。
[[按钮图层] setCornerRadius:8.0f];
您也可以尝试更改半径。
是的,当然可以。
您可以通过IB将单个操作连接到多个选择器。
您还可以直接从button1触发的方法中调用由button2触发的方法。
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (![super pointInside:point withEvent:event])
{
return NO;
}
BOOL isInside = (pow((point.x-self.frame.size.width/2), 2) + pow((point.y - self.frame.size.height/2), 2) < pow((self.frame.size.width/2), 2)) ? YES:NO;
return isInside;
}