iOSの加速度センサーからデバイスの傾きや向きを取得するサンプルを作成しました。
主な機能は下記の通りです。
主な機能
- X方向の傾き表示
- Y方向の傾き表示
- Z方向の傾き表示
- デバイスの向きを表示
以下よりサンプルのダウンロードが行えます。
サンプルダウンロード
ソースコード
ViewController.h
1
2
3
4
5
6
7
8
9
10
11
| #import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIAccelerometerDelegate> {
UILabel* _label;
float _aX;
float _aY;
float _aZ;
NSString* _orientation;
}
@end
|
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
| #import "ViewController.h"
#define FILTERING_FACTOR 0.1
@interface ViewController ()
@end
@implementation ViewController
//初期化
- (void)viewDidLoad
{
[super viewDidLoad];
//ラベルの生成
_label = [self makeLabel:CGPointMake(0, 0)
text:@"Accelerometer"
font:[UIFont systemFontOfSize:16]];
[self.view addSubview:_label];
//値の初期化
_aX = 0;
_aY = 0;
_aZ = 0;
_orientation = @"";
//加速度通知の開始
UIAccelerometer *accelermeter = [UIAccelerometer sharedAccelerometer];
accelermeter.updateInterval = 0.1f;
accelermeter.delegate = self;
//端末回転通知の開始
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
//ラベルのリサイズ
- (void)resizeLabel:(UILabel *)label
{
CGRect frame = label.frame;
frame.size = [label.text sizeWithFont:label.font
constrainedToSize:CGSizeMake(512, 512)
lineBreakMode:NSLineBreakByWordWrapping];
[label setFrame:frame];
}
//ラベルの生成
- (UILabel *)makeLabel:(CGPoint)pos
text:(NSString *)text
font:(UIFont *)font
{
UILabel *label = [[UILabel alloc] init];
[label setText:text];
[label setFont:font];
[label setTextColor:[UIColor blackColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:NSTextAlignmentLeft];
[label setNumberOfLines:0];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[self resizeLabel:label];
return label;
}
//端末の向きの取得
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
_orientation = @"横(左90度回転)";
} else if (orientation == UIDeviceOrientationLandscapeRight) {
_orientation = @"横(右90度回転)";
} else if (orientation==UIDeviceOrientationPortraitUpsideDown) {
_orientation = @"縦(上下逆)";
} else if (orientation == UIDeviceOrientationPortrait) {
_orientation = @"縦";
} else if (orientation == UIDeviceOrientationFaceUp) {
_orientation = @"画面が上向き";
} else if (orientation == UIDeviceOrientationFaceDown) {
_orientation = @"画面が下向き";
}
}
//加速度通知時に呼ばれる
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
//加速度にローパスフィルタをあてる
_aX = (acceleration.x * FILTERING_FACTOR) + (_aX * (1.0 - FILTERING_FACTOR));
_aY = (acceleration.y * FILTERING_FACTOR) + (_aY * (1.0 - FILTERING_FACTOR));
_aZ = (acceleration.z * FILTERING_FACTOR) + (_aZ * (1.0 - FILTERING_FACTOR));
//ラベルの更新
NSMutableString *str = [NSMutableString string];
[str appendString:@"Accelerometer\n\n"];
[str appendFormat:@"X軸加速度: %+.2f\n", _aX];
[str appendFormat:@"Y軸加速度: %+.2f\n", _aY];
[str appendFormat:@"Z軸加速度: %+.2f\n\n", _aZ];
[str appendFormat:@"端末の向き: %@", _orientation];
[_label setText:str];
[self resizeLabel:_label];
}
@end
|
お薦めの参考書
Cocos2d-xスマートフォン2Dゲーム開発講座 Cocos2d-x 3対応
Cocos2d-xを利用したスマートフォン向け3Dゲーム開発の手法を、サンプルを基に作りながら学ぶ事ができます。実際に遊べるゲームサンプルが4種収録されており、iOS / Android両対応のゲームを開発したい方には必見の一冊になっています。