我刚刚开始在 iPhone 上进行开发,并且我已经设法让我的按钮(其中六个)与 UIAccelerator 一起移动。当它们相互碰撞时我面临的问题。我希望它们向同一个方向反弹,如果我只使用 2 个按钮,这将有效。但是当有超过 2 个按钮的多次碰撞时,它们会重叠并最终相互融合。
这是我设置委托本身的代码:
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f/60.0f;
加速器的委托方式:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
delta.x = acceleration.x * 5;
delta.y = acceleration.y * -5;
self.firstButton.center = CGPointMake(self.firstButton.center.x + delta.x, self.firstButton.center.y + delta.y);
self.secondButton.center = CGPointMake(self.secondButton.center.x + delta.x, self.secondButton.center.y + delta.y);
self.thirdButton.center = CGPointMake(self.thirdButton.center.x + delta.x, self.thirdButton.center.y + delta.y);
self.fourthButton.center = CGPointMake(self.fourthButton.center.x + delta.x, self.fourthButton.center.y + delta.y);
self.fifthButton.center = CGPointMake(self.fifthButton.center.x + delta.x, self.fifthButton.center.y + delta.y);
self.sixthButton.center = CGPointMake(self.sixthButton.center.x + delta.x, self.sixthButton.center.y + delta.y);
[self updateMovingObjects:self.firstButton];
[self updateMovingObjects:self.secondButton];
[self updateMovingObjects:self.thirdButton];
[self updateMovingObjects:self.fourthButton];
[self updateMovingObjects:self.fifthButton];
[self updateMovingObjects:self.sixthButton];
}
更新移动对象:
- (void)updateMovingObjects:(UIButton *)button{
/*** DETECT COLLISIONS ***/
// Right screen
if(button.center.x < button.frame.size.width / 2){
button.center = CGPointMake(button.frame.size.width / 2, button.center.y);
}
// Left screen
if(button.center.x > 320 - button.frame.size.width / 2){
button.center = CGPointMake(320 - button.frame.size.width / 2, button.center.y);
}
// Top screen
if(button.center.y < button.frame.size.height / 2){
button.center = CGPointMake(button.center.x, button.frame.size.height / 2);
}
// Bottom screen
if(button.center.y > 480 - 20 - 43 - button.frame.size.height / 2){
button.center = CGPointMake(button.center.x, 480 - 20 - 43 - button.frame.size.height / 2);
}
// Detect collision with objects
for (UIView *anotherView in [self.view subviews]) {
if (button == anotherView){
continue;
}
if(CGRectIntersectsRect(button.frame, anotherView.frame)){
float_t vec = sqrtf(powf(delta.x, 2.0)+powf(delta.y, 2.0));
// collided from left or bottom
if( (button.center.x + button.frame.size.width / 2 >= anotherView.center.x - anotherView.frame.size.width / 2 &&
button.center.x + button.frame.size.width / 2 <= anotherView.center.x - anotherView.frame.size.width / 2 + 15)
|| (button.center.y - button.frame.size.height / 2 <= anotherView.center.y + anotherView.frame.size.height / 2 &&
button.center.y - button.frame.size.height / 2 >= anotherView.center.y + anotherView.frame.size.height / 2 - 15) ){
button.center = CGPointMake(button.center.x - vec, button.center.y + vec);
}
// collided from top or right
else{
button.center = CGPointMake(button.center.x + vec, button.center.y - vec);
}
}
}
/*** END DETECT COLLISIONS ***/
}
提前致谢。
编辑:我为检测它是否从左侧、右侧、底部或顶部碰撞的功能制作。现在工作得更好,但是如果我让它们同时移动,问题仍然存在重叠,我的猜测是它没有时间计算,因此被重叠:
- (BOOL)collidedFromLeft:(UIButton *)button{
for (UIView *anotherView in [self.view subviews]) {
if (button == anotherView){
continue;
}
if(CGRectIntersectsRect(button.frame, anotherView.frame)){
if(button.center.x + button.frame.size.width / 2 - delta.x < anotherView.center.x - anotherView.frame.size.width / 2 &&
button.center.x + button.frame.size.width / 2 >= anotherView.center.x - anotherView.frame.size.width / 2){
return YES;
}
}
}
return NO;
}
- (BOOL)collidedFromTop:(UIButton *)button{
for (UIView *anotherView in [self.view subviews]) {
if (button == anotherView){
continue;
}
if(CGRectIntersectsRect(button.frame, anotherView.frame)){
if(button.center.y + button.frame.size.height / 2 - delta.y < anotherView.center.y - anotherView.frame.size.height / 2 &&
button.center.y + button.frame.size.height / 2 >= anotherView.center.y - anotherView.frame.size.height / 2){
return YES;
}
}
}
return NO;
}
- (BOOL)collidedFromRight:(UIButton *)button{
for (UIView *anotherView in [self.view subviews]) {
if (button == anotherView){
continue;
}
if(CGRectIntersectsRect(button.frame, anotherView.frame)){
if(button.center.x - button.frame.size.width / 2 - delta.x > anotherView.center.x + anotherView.frame.size.width / 2 &&
button.center.x - button.frame.size.width / 2 <= anotherView.center.x + anotherView.frame.size.width / 2){
return YES;
}
}
}
return NO;
}
- (BOOL)collidedFromBottom:(UIButton *)button{
for (UIView *anotherView in [self.view subviews]) {
if (button == anotherView){
continue;
}
if(CGRectIntersectsRect(button.frame, anotherView.frame)){
if(button.center.y - button.frame.size.height / 2 - delta.y > anotherView.center.y + anotherView.frame.size.height / 2 &&
button.center.x - button.frame.size.width / 2 <= anotherView.center.y + anotherView.frame.size.height / 2){
return YES;
}
}
}
return NO;
}
然后我在他们移动时检查:
if([self collidedFromLeft:button]){
button.center = CGPointMake(button.center.x - 20, button.center.y);
}
if([self collidedFromRight:button]){
button.center = CGPointMake(button.center.x + 20, button.center.y);
}
if([self collidedFromTop:button]){
button.center = CGPointMake(button.center.x, button.center.y - 20);
}
if([self collidedFromBottom:button]){
button.center = CGPointMake(button.center.x, button.center.y + 20);
}