加速计和校准 - iPhone SDK

3

我需要在我的iPhone游戏中使用加速度计功能。我只需要通过倾斜设备来移动图像。然而,大多数YouTube视频仅显示某种倾斜功能,但忘记了包括校准。

我希望用户可以根据自己所处的位置进行校准。有没有人知道我应该如何开始呢?

1个回答

7
我曾经制作过类似的应用程序。我会在这里发布它,但它只适用于iOS 4...

校准:

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
2
抱歉,我要在23小时后才能给你赏金 :(. 但我会确保一定会给你的! - lab12
1
非常有用的答案。我想给你点赞,但是我已经达到了每日投票限制的上限:( - user269597
旧话题,但如果我们想从另一个视图控制器校准它,该怎么做?此外,这段代码是针对横向应用程序还是竖向的? - SimplyKiwi
在单独的类中实现UIAccelerometerDelegate,或通过委托将该方法调用到原始类。由于它使用用户默认设置,因此应该可以从任何类中访问。而且,它不受横向/纵向屏幕的影响。 - Glenn Smith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接