Ios-accelerometer
提供:Dev Guides
iOS-加速度計
加速度計は、x、y、zの3方向におけるデバイスの位置の変化を検出するために使用されます。 地面に対するデバイスの現在の位置を知ることができます。 この例をテストするには、 device で実行する必要があり、シミュレーターでは動作しません。
加速度計-関与するステップ
ステップ1 *-簡単な Viewベースのアプリケーション*を作成します。
ステップ2 *- *ViewController.xib に3つのラベルを追加し、xlabel、ylabel、zlabelという名前のibOutletsを作成します。
- ステップ3 *-次のようにViewController.hを更新します-
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIAccelerometerDelegate> {
IBOutlet UILabel *xlabel;
IBOutlet UILabel *ylabel;
IBOutlet UILabel *zlabel;
}
@end
ステップ4 *-次のように *ViewController.m を更新します-
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[UIAccelerometer sharedAccelerometer]setDelegate:self];
//Do any additional setup after loading the view,typically from a nib
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
(UIAcceleration *)acceleration {
[xlabel setText:[NSString stringWithFormat:@"%f",acceleration.x]];
[ylabel setText:[NSString stringWithFormat:@"%f",acceleration.y]];
[zlabel setText:[NSString stringWithFormat:@"%f",acceleration.z]];
}
@end
出力
*iPhone* デバイスでアプリケーションを実行すると、次の出力が得られます-