简体   繁体   中英

About touch coordinate between iPhone 4s, 5, 6 and 6+

Fyi, I am new to Xcode programming for iOS. I've written a simple game and tested on actual iPhone 4s and is working. As I only own iPhone 4s, I can only simulate my game with iOS simulator for higher iPhone version. The problem I observed for higher iPhone version is that the image is scaled, therefore the touch location is no longer valid.

PS: I do not use any UIButton . I am using CGRectMake to create the button and later declare the touch position as shown below.

-(void) touchBegan: (CGPoint) p 
{
    if (p.x > 0 && p.x < 100 && p.y > 0 && p.y < 30) myscore = 1;
}

So my question is, will the touch displacement seen in the simulator appear the same in the actual device? If yes, does it mean that I need to declare the touch boundary for each iPhone version?

If you want to fix the touch location regardless of the device you're using, the easier way is to, as said before, click and drag a Button on the storyboard and use Auto-Layout to fix the location of the button For instance, you can set the button to appear 10px down and 10px right from the top-left corner from the containing view and it will appear at the same location regardless of the device used. But I'm sure there's a way to programmatically do the exact same thing

PS: wanted to post a comment and not an answer, but I don't have enough rep...

Yes, this will happens in device to. As you comment you can set the bounds for each different device or (i think easier) use a UIButton. Why don't you use it ? Any special reason?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM