How to detect touch size in iOS

DISCLAIMER: This small tutorial describes not-so-documented API which could be considered as private API and, therefore, apps could be rejected by Apple’s App Store review team. You could vote to make this API public by filling enhancement radars.

Now, since we’re done with warnings, let’s see – is there any way to detect how big is touch size. In ideal world you would probably want to see the whole contact area.

Each UIView could handle touch events implementing -touchesBegan:, -touchesMoved:, -touchesEnded: and -touchesCancelled:. These methods get NSSet of UITouch objects.

UITouch itself does not have public properties to detect touch size. However, we can use KVC (key-value coding) to get some information.

-(void)touchesBegan:(NSSet *)touches 
          withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGFloat touchSize = [[touch valueForKey:@"pathMajorRadius"] floatValue];
    NSLog(@"touch size is %.2f", touchSize);
}

Basically, we’re not using any undocumeneted methods or classes. However, pathMajorRadius property itself is not public. It returns size of tap in millimeters. However, I would recommend to either calibrate to detect minimum and maximum tap size, or offer some adjustments in UI.

By the way, iOS simulator sets this property to value of 5.0.

How could this information be used? Imagine drawing app where you could control line thickness by pressing more or less.

Sample project is available on GitHub.

Leave a comment