2011-12-07 5 views
0

私は、テキストをモールス符号に変換し、iPhoneの懐中電灯を使ってフラッシュするアプリケーションを作成しています。私は文字列置換を使用して、NSStringの内容をモールス符号に変換しています。2つの間隔でNSTimerを使用する

// some of the code : 
    str = [str stringByReplacingOccurrencesOfString:@"5" withString:n5]; 
    str = [str stringByReplacingOccurrencesOfString:@"6" withString:n6]; 
    str = [str stringByReplacingOccurrencesOfString:@"7" withString:n7]; 
    str = [str stringByReplacingOccurrencesOfString:@"8" withString:n8]; 
    str = [str stringByReplacingOccurrencesOfString:@"9" withString:n9]; 
    str = [str stringByReplacingOccurrencesOfString:@"0" withString:n0]; 

    NSString *morseCode = [[NSString alloc] initWithFormat:str];   
    self.label.text = morseCode; 

iPhoneの懐中電灯をオン/オフするNSTimerを使用して調整可能な間隔でスクリプトを検出しました。しかし、2つの異なる間隔を追加する方法を理解することはできません。一つはドットに、もう一つはモールスダッシュに追加します。

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

int spaceTime; 
spaceTime = 1; 

int dashTime; 
dashTime = 2; 

int dotTime; 
dotTime = 0.8; 

strobeIsOn = NO; 
strobeActivated = NO; 
strobeFlashOn = NO; 

flashController = [[FlashController alloc] init]; 


self.strobeTimer =   [ 
          NSTimer 
          scheduledTimerWithTimeInterval:spaceTime 
          target:self 
          selector:@selector(strobeTimerCallback:) 
          userInfo:nil 
          repeats:YES 
          ]; 

self.strobeFlashTimer =  [ 
          NSTimer scheduledTimerWithTimeInterval:dotTime 
          target:self 
          selector:@selector(strobeFlashTimerCallback:) 
          userInfo:nil 
          repeats:YES 
          ]; 
    } 



- (void)strobeTimerCallback:(id)sender { 
if (strobeActivated) { 
    strobeIsOn = !strobeIsOn; 

    // ensure that it returns a callback. If no, returns only one flash 
    strobeFlashOn = YES; 
} else { 
    strobeFlashOn = NO; 
} 
} 

- (void)strobeFlashTimerCallback:(id)sender { 
if (strobeFlashOn) { 
    strobeFlashOn = !strobeFlashOn; 
    [self startStopStrobe:strobeIsOn]; 

} else { 
    [self startStopStrobe:NO]; 
} 
} 

私は2つのタイマーを使用する必要がありますか、または異なる間隔で1つを持つことはできますか?文字列の内容を配列に入れる必要がありますか? Obj-Cの新機能です。

答えて

0

私は、再帰関数を作ってみるでしょう。実際には、私にこれを行う方法

parseAndFlash 

{ 
    NSString *codeString = @"-.-. --- -.. ."; 
    int currentLetterIndex = 0; 
    //codeString and currentLetterIndex should be declared outside this function as members or something  

    double t_space = 2, t_point = 0.5, t_line = 1, t_separator = 0.1; 
    double symbolDuration = 0; 
    if(currentLetterIndex >= [codeString length]) 
     return; 

    char currentLetter = [codeString characterAtIndex:currentLetterIndex]; 
    switch (currentLetter) { 
     case '-': 
      symbolDuration = t_line; 
      [self flashOnFor:t_line]; 
      break; 
     case '.': 
      symbolDuration = t_point; 
      [self flashOnFor:t_point]; 
      break; 
     case ' ': 
      symbolDuration = t_space; 
      [self flashOff]; 
      break; 
     default: 
      break; 
    } 

    currentLetterIndex ++; 
    symbolDuration += t_separator; 
    [self performSelector:@selector(parseAndFlash) withObject:nil afterDelay:symbolDuration]; 

} 
+0

感謝します! .... :) – ebsp

0

バックグラウンドでコードを順番に実行し、必要なだけスリープしてみることができます。たくさんのタイマーを使うよりも、書いて維持する方がはるかに簡単なコードになります。

// execute in background 
[self performSelectorInBackground:@selector(doTheMagic) withObject:nil]; 

- (void)doTheMagic { 
    NSLog(@"Turn ON"); 
    [NSThread sleepForTimeInterval:1]; 
    NSLog(@"Turn OFF"); 
    [NSThread sleepForTimeInterval:0.1f]; 
    NSLog(@"Turn ON"); 
    [NSThread sleepForTimeInterval:1.0f]; 
    // ... 
} 
+0

を? – ebsp

+0

更新された回答。 – RolandasR