views:

14

answers:

1

If I have an object of arbitrary height and width that moves around the screen, what's an elegant way of detecting when it touches the edge of the screen?

A: 

This depends on your coordinate system. Assuming a coordinate system with (0|0) at the lower left corner you have those cases:

  • Touches left edge if object.origin.x <= 0
  • Touches right edge if object.origin.x + object.size.width >= screenSize.width
  • Touches bottom edge if object.origin.y <= 0
  • Touches top edge if object.origin.y + object.size.height >= screenSize.height

This also works for irregular shaped objects if you use the bounding rectangle for the object.

Sven
If you're using the origin then doesn't that allow the object to go out of the screen?
awakeFromNib