简体   繁体   English

iOS上的圆形按钮

[英]Circle button on iOS

Is it possible to create two button shown as below image? 是否可以创建如下图所示的两个按钮? It may seem like you should use UIButton or UIImageView but if i click to area 1, it stills acts as clicked to button 1. Button 2 should be fired when I click to the area 1 as well! 看起来你应该使用UIButton或UIImageView但是如果我点击区域1,它仍然可以点击按钮1.当我点击区域1时,按钮2应该被触发!

在此输入图像描述

Failing the above responses being acceptable, you could implement a custom subclass of UIButton that overrides pointInside:withEvent: . 如果上述响应未被接受,您可以实现UIButton的自定义子类,该子类重写pointInside:withEvent: .

Assuming your view is exactly square and the graphic exactly circular and filling the entire square, an example might be: 假设您的视图完全是正方形,图形完全是圆形并填充整个正方形,示例可能是:

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

You can make circular button by cutting the layer and set radius of you button. 您可以通过剪切图层并设置按钮的半径来制作圆形按钮。

[[button layer] setCornerRadius:8.0f]; [[button layer] setCornerRadius:8.0f];

you can also try with change radius. 您也可以尝试更改半径。

对于较小的按钮1,将userInteractionEnabled设置为NO。所有事件将转到较大的Button 2。

I have created two round rect buttons for this purpose, one of them is long and thin, and the other one is more wide. 我为此创建了两个圆形矩形按钮,其中一个是长而薄的,另一个是更宽的。 Together they build a shape like a chubby plus sign, which is pretty close to a circle, considering apple accepts 44 px as the minimum confortably pressable size. 他们一起构建了一个像胖乎乎的加号的形状,这非常接近圆形,考虑到苹果接受44像素作为最小的可压缩尺寸。 If you want the image to change, set another image to its imageView for highlighted state and connect several actions (touchup wont be sufficient if you want to immitate buttons highlighted state on the image view)of the two buttons. 如果您想要更改图像,请将另一个图像设置为其imageView以获得突出显示的状态,并连接多个操作(如果您想在图像视图上模仿按钮突出显示状态,则触摸不足)。 Alternatively you can add observers and alter highlighted state of the imageview according to the buttons 或者,您可以根据按钮添加观察者并更改图像视图的突出显示状态

A clean way to deal with pointInside on a circular button is this: 在圆形按钮上处理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;
}

You can ditch the 'isInside' variabe, but this way is easier to test. 您可以放弃'isInside'变量,但这种方式更容易测试。

Yes, of course is possible. 是的,当然是可能的。

You can connect a single action with more than one selector through IB. 您可以通过IB将单个操作与多个选择器连接起来。
You can also call directly the method fired by button2 from inside the method fired by button1. 您还可以从button1触发的方法内直接调用button2触发的方法。

It quite tricky, but possible: You can use only one button, but put some verification after event touchUpInside. 它非常棘手,但可能:您只能使用一个按钮,但在事件touchUpInside之后进行一些验证。 You should calculate if this touch point are inside the circle of "button1". 您应该计算此触摸点是否在“button1”的圆圈内。 For this task you need to have some mathematic knowledge - How do I calculate a point on a circle's circumference? 对于此任务,您需要具备一些数学知识 - 如何计算圆周长上的点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM