加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 加速度计和校准 – iPhone SDK

发布时间:2020-12-14 17:10:44 所属栏目:百科 来源:网络整理
导读:我需要在我的 iphone游戏中使用加速度计的功能.我只需要通过倾斜设备来移动图像.然而,youtube上的大多数视频只显示倾斜功能,以某种方式反转,忘记包括校准.我希望用户将他们的设备校准到他们所处的位置.有谁知道我应该如何开始这个? 非常感谢您的帮助, 凯文
我需要在我的 iphone游戏中使用加速度计的功能.我只需要通过倾斜设备来移动图像.然而,youtube上的大多数视频只显示倾斜功能,以某种方式反转,忘记包括校准.我希望用户将他们的设备校准到他们所处的位置.有谁知道我应该如何开始这个?

非常感谢您的帮助,

凯文

解决方法

我曾经做过这样的应用程序.
我会在这里发布,但它适用于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);
}

应该在大多数时间工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读