用加速度计摇动iphone!

时间:2010-08-05 12:52:09

标签: iphone sdk

嗨,由于某些原因,我不能使用摇动API,所以我通过加速度计使用摇动设备,但我不知道为什么iPhone 4,iPad不支持这个代码!但适用于所有设备..我不知道发生了什么! 这是我的代码:

#define kAccelerationThreshold        2.2
#define kUpdateInterval               (1.0f/10.0f)

@interface info : UIViewController  <UIAccelerometerDelegate> {

}
@end

~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~

@implementation info


- (void)viewDidLoad {
    UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
    accel.delegate = self;
    accel.updateInterval = kUpdateInterval;


    [super viewDidLoad];
}


#pragma mark -
- (void)accelerometer:(UIAccelerometer *)accelerometer 
        didAccelerate:(UIAcceleration *)acceleration {
   {
        if (acceleration.x > kAccelerationThreshold 
            || acceleration.y > kAccelerationThreshold
            || acceleration.z > kAccelerationThreshold) {

//What do you want to do !

                self.view.backgroundColor = [UIColor orangeColor];

        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您无法在iPhone上使用shake API,则可能未将视图控制器设置为viewDidAppear上的第一个响应者。在视图控制器上实现此方法:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

现在您将能够使用shake API。希望有所帮助:)