加速度计和校准 - iPhone SDK [英] Accelerometer and calibration - iPhone SDK

查看:35
本文介绍了加速度计和校准 - iPhone SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to use the functionality of an accelerometer in my iPhone game. I just have to move an image by tilting the device. However most videos on YouTube just show the tilt feature that is somehow inverted and forget to include the calibration.

我希望用户将他们的设备校准到他们所处的任何位置.有人知道我应该如何开始吗?

I want the user to calibrate their device to whatever position they're in. Does anyone know how I should get started on this?

推荐答案

我曾经做过一个这样的应用.我会在这里发布,但它适用于 iOS 4...

I made an app like this once. I'll post it here, but it's for 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);
}

应该大部分时间都可以工作.

Should work most of the time.

这篇关于加速度计和校准 - iPhone SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆