Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define custom touch area in custom UIControl object

Tags:

ios

uicontrol

I am creating a custom UIControl object as detailed here. It is all working well except for the touch area.

I want to find a way to limit the touch area to only part of the control, in the example above I want it to be restricted to the black circumference only rather than the whole control area.

Any idea? Cheers

like image 384
Abolfoooud Avatar asked Nov 24 '25 00:11

Abolfoooud


1 Answers

You can override UIView's pointInside:withEvent: to reject unwanted touches.

Here's a method that checks if the touch occurred in a ring around the center of the view:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    UITouch *touch = [[event touchesForView:self] anyObject];
    if (touch == nil)
        return NO;

    CGPoint touchPoint = [touch locationInView:self];
    CGRect bounds = self.bounds;

    CGPoint center = { CGRectGetMidX(bounds), CGRectGetMidY(bounds) };
    CGVector delta = { touchPoint.x - center.x, touchPoint.y - center.y };
    CGFloat squareDistance = delta.dx * delta.dx + delta.dy * delta.dy;

    CGFloat outerRadius = bounds.size.width * 0.5;

    if (squareDistance > outerRadius * outerRadius)
        return NO;

    CGFloat innerRadius = outerRadius * 0.5;

    if (squareDistance < innerRadius * innerRadius)
        return NO;

    return YES;
}

To detect other hits on more complex shapes you can use a CGPath to describe the shape and test using CGPathContainsPoint. Another way is to use an image of the control and test the pixel's alpha value.

All that depends on how you build your control.

like image 74
Nikolai Ruhe Avatar answered Nov 26 '25 19:11

Nikolai Ruhe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!