在我的应用程序中,我有两个按钮和一个计时器,可以每秒触发一个动作。每秒,它会生成四个随机数。这些数字将用作按钮的X和Y值,并且按钮将移动到新点。但是,有时新的点很接近,因此按钮会重叠。我如何检查以确保不会发生这种情况?我已经尝试检查按钮框架的CGRectIntersectsRect,但没有任何效果。
谢谢你的帮助!
发布于 2012-05-20 14:25:05
只是为了验证一下,你的CGRectIntersectsRect代码/结构看起来有点像下面的图:
-(void)update {
// This is the method that gets called every second where your four numbers are randomly generated
// Generate random numbers here
// Position the buttons based on 2 of the random numbers
if (CGRectIntersectsRect(button1.frame, button2.frame) {
// regenerate the four numbers
// reposition your buttons
}还有,我想知道,为什么每一秒?如果你愿意,可以在不同的线程上执行它,并将其设置为每秒10次,这样按钮重叠就不会显示太长时间,但这取决于你想要的速度。另外,如果你只使用两个(据我们所知),为什么你要生成4个随机数呢?
但是,最终我们需要看到你的代码,否则我们看不到你可能做错了什么。
https://stackoverflow.com/questions/10671182
复制相似问题