检测物体何时接触屏幕边缘
如果我有一个任意高度和宽度的对象在屏幕上移动,那么检测它何时接触屏幕边缘的优雅方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您的坐标系。假设坐标系的左下角为 (0|0),则有以下情况:
object.origin.x <= 0
则触摸左边缘object.origin.x 则触摸右边缘.x + object.size.width >= screenSize.width
object.origin.y <= 0
则触摸底部边缘object.origin.y
则触摸顶部边缘+ object.size.height >= screenSize.height
如果您使用对象的边界矩形,这也适用于不规则形状的对象。
This depends on your coordinate system. Assuming a coordinate system with (0|0) at the lower left corner you have those cases:
object.origin.x <= 0
object.origin.x + object.size.width >= screenSize.width
object.origin.y <= 0
object.origin.y + object.size.height >= screenSize.height
This also works for irregular shaped objects if you use the bounding rectangle for the object.