Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

enter image description here

like image 518
glockeruser Avatar asked Dec 28 '22 03:12

glockeruser


1 Answers

Failing the above responses being acceptable, you could implement a custom subclass of UIButton that overrides 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;
}
like image 199
Tommy Avatar answered Jan 16 '23 15:01

Tommy