A. 在 xxx project 的 .h 檔 Class 宣告裡, 新增協定: <UIAccelerometerDelegate>
B. 在 xxx project 的 .m 檔的 Class 定義裡:
// Constant for the number of times per second (Hertz) to acceleration.
#define kAccelerometerFrequency 50
....
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
....
// Configure and start the accelerometer
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self]; // .h => : <UIAccelerometerDelegate>
}
// UIAccelerometerDelegate method, called when the device accelerates.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
CGRect greenFrame = racquet_green.frame;
greenFrame.origin.x = greenFrame.origin.x + acceleration.x;
racquet_green.frame = greenFrame;
/*
NSLog(@"accelerometer");
NSLog(@"x:%f",acceleration.x); // -1 ~ 1
NSLog(@"y:%f",acceleration.y); // -1 ~ 1
NSLog(@"z:%f",acceleration.z);
*/
}
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。