0

私はオブジェクトCを初めて使用し、2つの質問がありますが、stackoverflowで答えを見つけることができません。

私のiOSアプリはシンプルで、画面上のボタンが1つあり、ユーザーがそれをタップすると、次のようになります。

  1. 音を出す

  2. 2タップ間の時間間隔をミリ秒で取得します。

Owlのおかげで、間隔を取得するコードは次のようになります。

(「UNIXタイムスタンプ」とは何かがわからず、2番目のコードをどこでどのように使用するかわからないため、長いコーディングです。)

double dt1;
double dt2;

-(IBAction)Beated:(id)sender{
   If (FB == 1) {
      FB = 2;
      NSDate *date = [NSDate date];
      NSTimeInterval ti = [date timeIntervalSince1970];
      dt1 = ti;
   } else {
      FB = 1
      NSDate *date = [NSDate date];
      NSTimeInterval ti = [date timeIntervalSince1970];
      dt2 = ti;
      double progress;
      progress = dt2 - dt1;
      int timeInMs = trunc(progress * 1000);
      NSLog(@"Interval %d", timeInMs);
   }
}

また、アプリを起動してから初めて音を鳴らしたときは遅れがありますが、最初のタップ以降は問題なく動作します。その遅れを止める方法は?

サウンドを再生するための私のコード:

.hで

#import <AVFoundation/AVFoundation.h>

AVAudioPlayer *audioPlayer;

.mで

 -(IBAction)Beated:(id)sender {
       NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ssn.wav",     [[NSBundle mainBundle] resourcePath]]];
       NSError*error;
       audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url  error:$error];
       audioPlayer.numberOfLoops = 0;
       [audioPlayer play];
}

ありがとう

4

1 に答える 1

1

最初のタップ、

NSDate *date = [NSDate date];
NSTimeInterval ti = [date timeIntervalSince1970];

UNIXタイムスタンプを取得します。

2回目のタップ、

NSDate *date = [NSDate date];
NSTimeInterval ti = [date timeIntervalSince1970];

別のUNIXタイムスタンプを取得してから、2番目のタイムスタンプから最初に減算します。引き算の積があなたの進歩になります。

次に、このコードを使用して、時:分:秒を取得します

double progress;

 int minutes = floor(progress/60);
 int seconds = trunc(progress - minutes * 60);

コード提供:NSTimeInterval(秒)を分に変換する方法

より簡単に

2つのタップから2つのNSDateを取得してから使用します。その後、減算は必要ありません。以下のメソッドを使用して時間間隔を取得します。次に、上記のように分と秒を計算します。

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate

あるいは

クラスのcomponents:fromDate:toDate:options:メソッドを使用します。NSCalender参照:AppleDoc

編集 :

Jusは簡単なテストを行い、それは私にとって完璧に機能しました。

テストコード:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    NSDate *date = [NSDate date];
    NSTimeInterval ti = [date timeIntervalSince1970];

    NSLog(@"%f",ti);

    return YES;
}

NSLog出力:

2012-08-22 10:46:09.123 Test[778:c07] 1345596369.123665
于 2012-08-22T00:22:50.407 に答える