当某些事情发生时,IPhone sdk阻止加速度计工作

时间:2010-04-02 11:31:52

标签: iphone sdk accelerometer void boolean

我正在使用加速度计在屏幕周围移动一些UIImageViews,此时加速度计仅在NSTimer为0时才起作用,这很好。当发生不同的功能时,我还想让加速度计再次停止。

此处是我的代码:

-(BOOL) accelerometerWorks {
 return time == 0;
}

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
 if(![self accelerometerWorks]) return;
 valueX = acceleration.x*25.5;



 int newX = (int)(ball.center.x +valueX);
 if (newX > 320-BALL_RADIUS)
  newX = 320-BALL_RADIUS;
 if (newX < 0+BALL_RADIUS)
  newX = 0+BALL_RADIUS;


 int XA = (int)(balloonbit1.center.x +valueX);
 if (XA > 320-BALL_RADIUS)
  XA = 320-BALL_RADIUS;
 if (XA < 0+BALL_RADIUS)
  XA = 0+BALL_RADIUS;


 int XB = (int)(balloonbit2.center.x +valueX);
 if (XB > 320-BALL_RADIUS)
  XB = 320-BALL_RADIUS;
 if (XB < 0+BALL_RADIUS)
  XB = 0+BALL_RADIUS;

 int XI = (int)(balloonbit3.center.x +valueX);
 if (XI > 320-BALL_RADIUS)
  XI = 320-BALL_RADIUS;
 if (XI < 0+BALL_RADIUS)
  XI = 0+BALL_RADIUS;

 int XJ = (int)(balloonbit4.center.x +valueX);
 if (XJ > 320-BALL_RADIUS)
  XJ = 320-BALL_RADIUS;
 if (XJ < 0+BALL_RADIUS)
  XJ = 0+BALL_RADIUS;


 int XE = (int)(balloonbit5.center.x +valueX);
 if (XE > 320-BALL_RADIUS)
  XE = 320-BALL_RADIUS;
 if (XE < 0+BALL_RADIUS)
  XE = 0+BALL_RADIUS;


 int XF = (int)(balloonbit6.center.x +valueX);
 if (XF > 320-BALL_RADIUS)
  XF = 320-BALL_RADIUS;
 if (XF < 0+BALL_RADIUS)
  XF = 0+BALL_RADIUS;


 int XH = (int)(balloonbit8.center.x +valueX);
 if (XH > 320-BALL_RADIUS)
  XH = 320-BALL_RADIUS;
 if (XH < 0+BALL_RADIUS)
  XH = 0+BALL_RADIUS;


 ball.center = CGPointMake (newX, 429);
 balloonbit1.center = CGPointMake (XA, 429);
 balloonbit2.center = CGPointMake (XB, 426);
 balloonbit3.center = CGPointMake (XI, 410);
 balloonbit4.center = CGPointMake (XJ, 415);
 balloonbit5.center = CGPointMake (XE, 409);
 balloonbit6.center = CGPointMake (XF, 427);
 balloonbit8.center = CGPointMake (XH, 417);

}

这一切都运行正常,我现在想停止加速度计工作,使用相同的

accelerometerWorks方法或新方法,accelerometerWontWork。

这是代码,当它被调用时,我希望加速度计停止。 (它很长,所以我不会把它全部放在那里,但它始终都是一样的):

-(void) checkCollision{

 if (CGRectIntersectsRect(pinend.frame, balloonbit1.frame)){
  if (balloonbit1.frame.origin.y > pinend.frame.origin.y){
   [maintimer invalidate];
   levelfailed.center = CGPointMake (160, 235);
  }
 }
 if (CGRectIntersectsRect(pinend.frame, balloonbit2.frame)){
  if (balloonbit2.frame.origin.y > pinend.frame.origin.y){
   [maintimer invalidate];
   levelfailed.center = CGPointMake (160, 235);
  }
 }
 if (CGRectIntersectsRect(pinend.frame, balloonbit3.frame)){
  if (balloonbit3.frame.origin.y > pinend.frame.origin.y){
   [maintimer invalidate];
   levelfailed.center = CGPointMake (160, 235);
  }
 }
}

我知道我可能需要将其转换为BOOL。太好了。

如果我没有解释清楚,请说出来,我会重写。

谢谢! 哈利。

1 个答案:

答案 0 :(得分:7)

accelManeger = [UIAccelerometer sharedAccelerometer];
accelManeger.delegate = nil;

会阻止您接收活动。然后在想要重新开始时将自己设置为委托。