Search code examples
iphoneiosipaduitouchtouchesbegan

How do I determine what the valid range is for CGPoint on a device when using locationInView of touch events?


I know the iPhone has 480 x 320 and iPad is 1024 x 768 but I don't know how to determine that programmatically. I'd appreciate any help I can get.


Solution

  • UITouch*yourTouchNameHere = [touches anyObject];
    CGPoint yourPointNameHere=[touch locationInView:self.view];
    

    This automatically means that the touch is on the screen, so any touch-point returned would be valid, but still the code below might help your understanding:


    Receive Screen size with: [UIScreen mainScreen].bounds.size

    Height of iOSDevice in pixels returned with:

    [UIScreen mainScreen].bounds.size.height
    

    Width of iOSDevice in pixels returned with:

    [UIScreen mainScreen].bounds.size.width
    

    Is the iOSDevice an iPhone with:

    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){}
    

    Is the iOSDevice an iPad with:

    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){}
    

    Another stackOverflow question here shows you could also use

    Return the entire screen resolution in points (320x400, etc.) with:

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    

    Return the screen resolution scale with:

    CGFloat screenScale = [[UIScreen mainScreen] scale];
    

    Another way to get the pixel width and height with the two above lines included:

    CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);