2010-06-15 88 views
3

我需要在我的iphone遊戲中使用加速度計的功能。我只需通過傾斜設備來移動圖像。但是,YouTube上的大多數視頻僅顯示以某種方式倒置的傾斜功能,並忘記包含校準。我希望用戶能夠將他們的設備校準到他們所在的任何位置。有誰知道我應該如何開始?加速計和校準 - iPhone SDK

你的幫助是極大的讚賞,

凱文

回答

7

我犯了這樣的一個應用程序一次。 我將它張貼在這裏,但它是爲iOS 4 ...

http://localhostr.com/files/7ca39c/Tilter.zip

校準:

int tapCount = 0; 
- (void)cancelDoubleTap { 
    if (tapCount < 2) { 
     tapCount = 0; 
    } 
} 
- (void)reset { 
    [[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"X-Calibrate"]; 
    [[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"Y-Calibrate"]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reset!" message:@"Calibration reset." delegate:nil cancelButtonTitle:@"Okay." otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
- (void)calibrate { 
    if (tapCount == 3) { 
     tapCount = 0; 
     return; 
    } 
    [[NSUserDefaults standardUserDefaults] setFloat:accelX forKey:@"X-Calibrate"]; 
    [[NSUserDefaults standardUserDefaults] setFloat:accelY forKey:@"Y-Calibrate"]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Calibrated!" message:[NSString stringWithFormat:@"Calibrated to: %.2f %.2f.", accelX, accelY] delegate:nil cancelButtonTitle:@"Okay." otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    tapCount = 0; 
} 
- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(cancelDoubleTap) userInfo:nil repeats:NO]; 
    tapCount ++; 
    if (tapCount == 3) { 
     [timer invalidate]; 
     [self reset]; 
     return; 
    } 
    if (tapCount == 2) { 
     [timer invalidate]; 
     timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(calibrate) userInfo:nil repeats:NO]; 
    } 
} 

而且......

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    but = [[UIButton alloc] initWithFrame:window.frame]; 
    [but addTarget:self action:@selector(touchesEnded:withEvent:) forControlEvents:UIControlEventTouchUpInside]; 
    [window addSubview:but]; 
    // Override point for customization after application launch. 
    box = [[UIView alloc] initWithFrame:CGRectMake(self.window.center.x - 50, self.window.center.y - 50, 100, 100)]; 
    [window makeKeyAndVisible]; 
    box.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.75]; 
    [window addSubview:box]; 
    [[UIAccelerometer sharedAccelerometer] setDelegate:self]; 
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1/100.0)]; 
    slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 438, 280, 22)]; 
    [slider setMinimumValue:2.0]; 
    [slider setMaximumValue:50.0]; 
    [window addSubview:slider]; 
    return YES; 
} 

.h文件中:

#import <UIKit/UIKit.h> 

@interface TilterAppDelegate : NSObject <UIApplicationDelegate, UIAccelerometerDelegate> { 
    UIWindow *window; 
    UIView *box; 
    UISlider *slider; 
    UIButton *but; 
} 

@property (nonatomic, retain) IBOutlet UIButton *but; 
@property (nonatomic, retain) IBOutlet UISlider *slider; 
@property (nonatomic, retain) IBOutlet UIView *box; 
@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end 

移動代碼:

- (void)moveBoxByX:(float)x byY:(float)y { 
    float newX = box.center.x + x; 
    float newY = box.center.y + y; 
    if (newX < 50) 
     newX = 50; 
    if (newX > 270) 
     newX = 270; 
    if (newY < 50) 
     newY = 50; 
    if (newY > 430) 
     newY = 430; 
    box.center = CGPointMake(newX, newY); 
} 

大部分時間應該工作。

+2

謝謝!我也是一名開發人員,所以我也有4.0固件。我會檢查出來的! – lab12 2010-06-17 19:46:56

+0

不客氣! – 2010-06-17 19:48:41

+1

這是完美的!再次感謝你! – lab12 2010-06-17 19:56:36