iphoneのアプリ作りで悩んでます。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
yamachan

iphoneのアプリ作りで悩んでます。

#1

投稿記事 by yamachan » 9年前

はじめまして、iphoneアプリ開発初心者です。
加速度センサーを使うアプリを作ろうとしています。
加速度を60秒はかって自動的に止めたいんですが、とりあえずタイマー機能を追加したところ、エラーは出てないのですがタイマーが起動されていないようです。なぜ起動されないのか悩んでいます。
また、60秒後に自動的に停止する方法でほかの方法があるのでしょうか...詳しい方がいたら教えてください。

#import "ViewController.h"

@interface ViewController (){
NSTimer *mainTimer;
}

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

BOOL btnFlag = false;

- (void)viewDidLoad
{
[super viewDidLoad];

[self redirectConsoleLogToDocumentFolder];

self.xLabel = [[UILabel alloc] init];
self.xLabel.frame = CGRectMake(20, 30, 150, 20);
[self.view addSubview:self.xLabel];
self.yLabel = [[UILabel alloc] init];
self.yLabel.frame = CGRectMake(20, 60, 150, 20);
[self.view addSubview:self.yLabel];
self.zLabel = [[UILabel alloc] init];
self.zLabel.frame = CGRectMake(20, 90, 150, 20);
[self.view addSubview:self.zLabel];
self.rsltLabel = [[UILabel alloc] init];
self.rsltLabel.frame = CGRectMake(20, 120, 300, 20);
[self.view addSubview:self.rsltLabel];

btnFlag = false;
self.startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.startBtn.frame = CGRectMake(110, 420, 100, 100);
self.startBtn.layer.cornerRadius = 50;
[self.startBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
[[self.startBtn layer] setBorderWidth:1.0f];
[self.startBtn addTarget:self action:@selector(startAccelerometer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.startBtn];
[self.startBtn setTitle:@"START" forState:UIControlStateNormal];
[self.startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

_motionManager = [[CMMotionManager alloc] init];

startTime = [NSDate timeIntervalSinceReferenceDate];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
target: self
selector:@selector(TimerAction)
userInfo:nil
repeats:YES];
mainTimer = timer;
counter = 60.0;
}

- (void)startAccelerometer:(UIButton *)button
{
if (btnFlag == false)
{
counter = 0;
XSum = 0;
YSum = 0;
ZSum = 0;
xSum = 0;
ySum = 0;
zSum = 0;
[self.startBtn setTitle:@"STOP" forState:UIControlStateNormal];
NSLog(@"------------------------------------");

if (_motionManager.accelerometerAvailable)
{
_motionManager.accelerometerUpdateInterval = 1 / 10; // 10Hz

CMAccelerometerHandler handler = ^(CMAccelerometerData *data, NSError *error)
{
self.xLabel.text = [NSString stringWithFormat:@"X-> %f", data.acceleration.x];
self.yLabel.text = [NSString stringWithFormat:@"Y-> %f", data.acceleration.y];
self.zLabel.text = [NSString stringWithFormat:@"Z-> %f", data.acceleration.z];

counter++;
xSum += data.acceleration.x;
ySum += data.acceleration.y;
zSum += data.acceleration.z;
XSum = 9.8 - xSum / 20;
YSum = 9.8 - ySum / 20;
ZSum = 9.8 - zSum / 20;


NSLog(@"X:%f Y:%f Z:%f", data.acceleration.x, data.acceleration.y, data.acceleration.z);
};
[_motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:handler];
btnFlag = true;
}
} else {
[self.startBtn setTitle:@"START" forState:UIControlStateNormal];
[_motionManager stopAccelerometerUpdates];

self.rsltLabel.text = [NSString stringWithFormat:@"Result-> X:%.2f Y:%.2f Z:%.2f",
xSum/counter, ySum/counter, zSum/counter];


btnFlag = false;
self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.nextBtn.frame = CGRectMake(110, 518, 236, 0);
[self.nextBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
[[self.nextBtn layer] setBorderWidth:1.0f];
[self.startBtn addTarget:self action:@selector(hoge:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.nextBtn];
[self.nextBtn setTitle:@"次へ>>" forState:UIControlStateNormal];
[self.nextBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

}-(void)TimerAction{
if(countDown>0){
countDown--;
[_timeLabel setText:[NSString stringWithFormat:@"%d",countDown]];
}else{
[timer invalidate]; // タイマーを停止する
NSLog(@"---------タイムオーバ-----------");
}}


- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];

if (_motionManager.accelerometerActive) {
[_motionManager stopAccelerometerUpdates];
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - ConsoleLogDataSource

- (void) redirectConsoleLogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}

@end

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: iphoneのアプリ作りで悩んでます。

#2

投稿記事 by h2so5 » 9年前

コード:

- (void)TimerAction {

コード:

- (void)TimerAction:(NSTimer *)timer {
に変えてください。

CoolApp

Re: iphoneのアプリ作りで悩んでます。

#3

投稿記事 by CoolApp » 9年前

内容がトピックの内容と著しく外れていましたのでofftopicとしました(管理人)
urlも無いし意味不明・・・。
オフトピック
はじめまして!
無料でアプリを簡単に作成できるツールを公開中です。
よかったらぜひお試しください(^^)
また遊びにきます!
アプリ&ブログがんばってくださーい

閉鎖

“C言語何でも質問掲示板” へ戻る