加速度计和校准 - iPhone SDK

2024-05-06

我需要在 iPhone 游戏中使用加速计的功能。我只需通过倾斜设备来移动图像即可。然而,YouTube 上的大多数视频仅显示以某种方式反转的倾斜功能,而忘记包含校准。

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


我曾经做过一个这样的应用程序。我会在这里发布它,但它是针对 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];
    }  
}

And...

 - (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);
}

大部分时间应该都在工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

加速度计和校准 - iPhone SDK 的相关文章

随机推荐

  • mysqli_query() 需要至少 2 个参数,其中 1 个参数在? [复制]

    这个问题在这里已经有答案了 每次运行这个 php ini 时 我都会遇到同样的 3 个错误 我不知道我做错了什么 有人可以帮忙吗 以下是错误 2014 年 5 月 5 日 19 20 50 美洲 芝加哥 PHP 警告 mysqli quer
  • 我们如何针对 DOM 操作执行单元测试?

    QUnit 的介绍位于netTuts com http net tutsplus com tutorials javascript ajax how to test your javascript code with qunit 关于如何针
  • tsconfig.json:构建:在配置文件中找不到输入

    我有一个 ASP NET core 项目 当我尝试构建它时收到此错误 error TS18003 Build No inputs were found in config file Z Projects client ZV src ZV S
  • 如何通过成员函数指针进行调用?

    我正在尝试使用成员函数指针进行一些测试 这段代码有什么问题 这bigCat pcat 语句无法编译 class cat public void walk printf cat is walking n int main cat bigCat
  • Tensorflow:docker 镜像和 -gpu 后缀

    在具有 GPU 支持的 Tensorflow 的 Docker 映像中 例如 tensorflow tensorflow 2 2 0 gpu 安装的python包是tensorflow gpu 如图所示pip freeze 安装任何依赖于的
  • 为什么 Visual Studio 使用 xchg ax,ax

    我正在查看程序的反汇编 因为它崩溃了 并注意到很多 xchg ax ax 我用谷歌搜索了一下 发现它本质上是一个 nop 但是为什么 Visual Studio 会执行 xchg 而不是 noop 该应用程序是一个C NET3 5 64位应
  • 导入错误:没有名为 PyQt4 的模块

    我使用 Homebrew 安装了 pyqt4 但是当我在 python 解释器中导入 PyQt4 时 它说 没有名为 PyQt4 的模块 有人可以帮我吗 After brew install pyqt 你可以brew test pyqt它将
  • 如何将java库添加到kotlin本机

    所以我尝试使用intellij创建kotlin native应用程序 我在项目创建中选择了模板kotlin gt kotlin native 它创建了示例 gradle hello world 项目 下载所有依赖项后编译为exe文件并正常运
  • "$(document).on('pageshow'" 不适用于 jQuery 1.9.1 + JQM 1.3.0-stable

    使用 jQuery 1 8 3
  • 选项卡式导航

    我真的很难弄清楚如何执行以下操作 我想要有两个选项卡 水平相邻 一个用于搜索 并如此标记 另一个用于帖子 如此标记 当选择搜索选项卡时 我希望出现一个搜索框 当选择帖子选项卡时 我希望出现另一个搜索框 我不想隐藏搜索框 我猜它本质上是使用
  • 从 Json Python 获取特定字段值

    我有一个 JSON 文件 我想做的是获取这个特定字段 id 问题是当我使用json load input file 它说我的变量data是一个列表 而不是字典 所以我不能做类似的事情 for value in data id print d
  • Jquery 事件处理程序返回值

    返回值有什么用处吗 click and change 处理程序 如return true or return false return false 将阻止事件冒泡AND抑制默认操作 换句话说 返回 false 类似于这两行 event st
  • 获取已连接 USB 设备的端口名称

    当USB设备连接到计算机时 如何使用C 代码获取它所连接的端口名称 我找到了很多方法来查找 USB 何时连接 断开 驱动器号 路径 设备 ID 等 但没有找到任何明确的示例来说明如何知道它连接到哪个端口 我看到了一种可能的解释 但这涉及很多
  • MbUnit v3 中的UsingFactories 替代方案

    我试图弄清楚如何在 MbUnit v3 中编写组合测试 网上的所有示例代码均参考MbUnit v2 这意味着使用3个属性 组合测试 Factory 使用工厂 在 MbUnit v3 中 没有 usingFactories 属性 并且 Fac
  • 删除networkx有向图中入度和出度等于1的所有节点

    假设我在 NetworkX 中制作了一个有向图 import networkx as nx G nx DiGraph n A B C D E F H I J K L X Y Z e A Z Z B B Y Y C C G G H G I I
  • 算法:最大计数器

    我有以下问题 您有 N 个计数器 最初设置为 0 并且您对它们有两种可能的操作 increase X 计数器 X 加 1 max counter 所有计数器都设置为任何计数器的最大值 给出一个包含 M 个整数的非空零索引数组 A 该数组代表
  • 修改 Android 可绘制对象的颜色

    我希望能够使用相同的可绘制对象来表示两者 and 作为相同的可绘制对象 并根据某些编程值重新为可绘制对象着色 以便最终用户可以重新主题化界面 做这个的最好方式是什么 我已经尝试过 并重复使用了其中的图标 这个以前的S O 问题 https
  • SecurityApplicationGroupIdentifier 的应用程序组返回 nil

    我设置了一个应用程序组与我的 Today Extension 一起使用 我在主应用程序的目标和扩展程序的目标中添加了应用程序组 在开发人员门户的应用程序 ID 配置中 我启用了应用程序组 但由于某种原因FileManager default
  • 在 IEnumerable 上使用 Seq 函数 [重复]

    这个问题在这里已经有答案了 我正在尝试在 IEnumerable 上应用 Seq 函数 更具体地说 它是System Windows Forms HtmlElementCollection它实现了ICollection and IEnume
  • 加速度计和校准 - iPhone SDK

    我需要在 iPhone 游戏中使用加速计的功能 我只需通过倾斜设备来移动图像即可 然而 YouTube 上的大多数视频仅显示以某种方式反转的倾斜功能 而忘记包含校准 我希望用户将他们的设备校准到他们所处的任何位置 有谁知道我应该如何开始 我