Search code examples
iostouch-eventuicontrol

Overlapping UIControls


I have two custom UIControls. These are scrolling wheels.

Their frames overlap. On the screen capture, the one on the top left is behind the one on the right. How may I begin the tracking on the left one when the touch is initiated inside the overlapping region ?

My beginTrackingWithTouch looks like this:

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
if (!insideCircle) return NO; } // pseudo-code

How can the event be propagated to the views behind the UIControls that respond NO ?

overlapping frames


Solution

  • My suggestion is:
    1) Subclass UIView for your wheel, for example WheelView;
    2) Override -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
    For example:

    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
    {
        CGFloat xOffset = (point.x - self.bounds.size.width / 2);
        CGFloat yOffset = (point.y - self.bounds.size.height / 2);
        CGFloat distanceSqr = xOffset * xOffset + yOffset * yOffset;
        if (distanceSqr > radius * radius)
        {
            // touch point is outside of circle
            return NO;
        }
        return YES;
    }
    

    So when you will return NO, touch will be propagated to next view, which is "below". This won't however work when your circles will overlap - only when frames, as shown on your screenshot.