开发者

IPHONE - detecting if a pointer is inside a coordinate

开发者 https://www.devze.com 2022-12-17 07:43 出处:网络
I have an object that can be moved across the screen with the finger. This object is an image, a small image, like a thumbnail.

I have an object that can be moved across the screen with the finger. This object is an image, a small image, like a thumbnail.

On the screen I have a background image where 10 rectangles were drawn. This rectangles are part of the background image. The background image is dumb, just a UIImageView.

I have 10 sounds I want to play every time the thumbnail passes over one of the 10 areas, represented by the 10 rectangles on the background. Each area has its own sound.

All I have is the size of the translating thumbnail and its coordinat开发者_运维技巧es (like origin, center, width and height). I have the origin (x and y) coordinates in realtime.

The point is: how to detect if the translating thumbnail is over one of the 10 squares considering a certain tolerance (example +- 10 pixels) and discover what area is it?

The problem: as I have the origin coordinates in realtime I can always create a loop to check if this value is inside one of the 10 rectangles, but this is CPU intensive because the loop will run for each pixel the thumbnail scrolls.

Any other ideas on how to do that?

thanks for any help.


You could poll for the coordinates at a predefined interval instead of constantly.

The idea behind this is in the main loop to set off a (say 1 second) timer. When the timer finishes it fires an event in which you can inquire the current location. Then use that value to check to see which rectangle it's in.


I would use a timer to fire a method which checks them every .2-.5 seconds:

[NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(checkPointInRects) userInfo:nil repeats:YES];

Use touchesBegan,moved,ended to cache the current touches and refer to them in the checkPointInRects method. You can use CGRectContainsPoint to determine if the point lies in any given rectangle.

0

精彩评论

暂无评论...
验证码 换一张
取 消