13

I want to find the pressure of the touch. I just don't know how to accomplish that result with out jailbreaking it and getting the raw touch data. Does anyone know How I would do this?

2
  • 6
    So how do you detect if the screen is broken then?
    – johndodo
    Jan 8, 2012 at 13:43
  • apple must use something like this to determine the velocity in the garage band app.
    – Jordan
    Jan 25, 2012 at 4:44

4 Answers 4

28

You cannot get the pressure from the SDK nor undocumented methods. However you can detect the size of touch with undocumented methods.


In the GSEvent, which is a lower-level representation of UIEvent, there is a structure known as GSPathInfo with members:

typedef struct GSPathInfo {
    unsigned char pathIndex;        // 0x0 = 0x5C
    unsigned char pathIdentity;     // 0x1 = 0x5D
    unsigned char pathProximity;    // 0x2 = 0x5E
    CGFloat pathPressure;               // 0x4 = 0x60
    CGFloat pathMajorRadius;        // 0x8 = 0x64
    CGPoint pathLocation;           // 0xC = 0x68
    GSWindowRef pathWindow;         // 0x14 = 0x70
} GSPathInfo;

We notice that there is a pathPressure and pathMajorRadius. I can assure you the pressure member is useless – it always gives 0. However pathMajorRadius does contain meaningful information – it gives the major radius of the touch in millimeters. You can therefore give an extremely rough estimation if it's a heavy touch or light touch from the radius.

  -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
     GSEventRef gsevent = [event _gsEvent];
     GSPathInfo first_touch = GSEventGetPathInfoAtIndex(gsevent, 0);
     if (first_touch.pathMajorRadius >= 9)
        NSLog(@"huge (heavy) touch");
     else
        NSLog(@"little (light) touch");
  }

Let me warn you again this is undocumented and you should not use it in AppStore apps.


Edit: On 3.2 and above the pathMajorRadius of a GSPathInfo is also available as an undocumented property in UITouch:

@property(assign, nonatomic, setter=_setPathMajorRadius:) CGFloat _pathMajorRadius;

so the code above could be rewritten using pure Objective-C:

  -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
     UITouch* any_touch = [touches anyObject];
     if (any_touch._pathMajorRadius >= 9)
        NSLog(@"huge (heavy) touch");
     else
        NSLog(@"little (light) touch");
  }
7
  • Would apple reject the app for that reason. Because it is undocumented?
    – Jab
    Jan 20, 2010 at 18:09
  • 2
    @Jaba: Yes. To use GSEventGetPathInfoAtIndex you'll need to link to the private framework GraphicsServices. Even if you do late binding, Apple will still find the strings _gsEvent and GSEventGetPathInfoAtIndex and reject that.
    – kennytm
    Jan 20, 2010 at 18:12
  • Is there a backdoor that I could explore, like another way to get this to work, or could I access this private file through another pathway?
    – Jab
    Jan 20, 2010 at 18:16
  • Wow, that's pretty damn cool. But if you use it you can guarantee yourself a nice little rejection.
    – Alex Wayne
    Jan 20, 2010 at 18:32
  • 1
    @yonix: Probably it uses jminor's method.
    – kennytm
    Oct 23, 2011 at 20:31
13

As of iOS 8.0, UITouch has a public majorRadius property which tells you the approximate size of the touch.

12

In iOS 3.2 and 4.0 you can get the value more directly like this:

UITouch* touch = ...// get the touch object
float radius = [[touch valueForKey:@"pathMajorRadius"] floatValue];

Still not App Store approved, but handy for custom stuff.

2
  • 1
    What do you mean still not App Store approved? Will Apple reject my app if I use it? (if so, how do apps like Virtuoso Piano achieve this?)
    – yonix
    Oct 24, 2011 at 8:37
  • 3
    Some apps try to use the accelerometer to measure how "hard" the user taps the screen. I'm not sure about Virtuoso Piano. As far as I know there is still no Apple review policy approved way to get at the touch radius information described in this article.
    – jminor
    Nov 2, 2011 at 23:21
1

It's not possible with the SDK. It does not expose that data in anyway. And the screen does not sense pressure, and wether it even reports touch "size" to the OS is unknown. So sadly, what you are trying to do is not possible on a legit application.

I know because I asked the same thing: Can the iPhone detect the size of a touch?

2
  • It does sense the size of the touch but the information is discarded. See my answer.
    – kennytm
    Jan 20, 2010 at 18:04
  • @Kenny: where is your answer and can i access the discarded information, and use it in the app without it being rejected?
    – Jab
    Jan 20, 2010 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.