2012-03-26 102 views
4

是否可以创建如下图所示的两个按钮?它可能看起来像你应该使用UIButton或UIImageView,但如果我点击区域1,它仍然作为点击按钮1.按钮2应该被解雇,当我点击区域1以及!iOS上的圆形按钮

enter image description here

+2

除非你需要两个动作,为什么不把他们作为一个单一的图像装入一个看不见的按钮? – 2012-03-26 12:11:10

回答

0

是的,当然是可以的。

您可以通过IB有多个选择连接一个动作。
您也可以直接从button1触发的方法内部调用button2触发的方法。

3

你可以通过你的切割按钮的层,并设置半径作圆形状的按钮。

[[按钮层] setCornerRadius:8.0f];

你也可以尝试改变半径。

+0

设置cornerRadius只影响图像,area1仍然可以点击顶部按钮 – Bot 2014-07-14 21:14:50

0

设置userInteractionEnabled为NO小按钮1.所有事件将进入更大的按钮2.

0

我已经创建了两个圆矩形按钮用于此目的,其中之一是长而薄,另一个更宽。他们一起构建了一个胖乎乎的加号形状,这个形状非常接近圆形,考虑到苹果可以接受44像素作为最小可压缩尺寸。如果要更改图像,请将另一图像设置为其imageView以显示突出显示的状态,并将两个按钮的多个操作(如果想要在图像视图中模仿按钮高亮显示的状态,则不会足够)。或者,您可以添加观察者并根据按钮更改imageview的突出显示状态

7

如果未能将上述响应接受,您可以实施替代pointInside:withEvent:UIButton自定义子类。

假设你的视图正好是正方形和图形精确的圆形和填充整个方形,其示例可以是:

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

干净的方式来处理pointInside上的圆形按钮是这样的:

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

您可以沟“isInside” variabe,但这种方式更容易进行测试。