Objective c 使用具有两个间隔的NSTimer
我正在创建一个应用程序,将文本转换成莫尔斯电码,然后用iPhone的手电筒将其闪烁出来。我使用了字符串替换,将NSString的内容转换为摩尔斯电码Objective c 使用具有两个间隔的NSTimer,objective-c,ios,xcode,cocoa,nstimer,Objective C,Ios,Xcode,Cocoa,Nstimer,我正在创建一个应用程序,将文本转换成莫尔斯电码,然后用iPhone的手电筒将其闪烁出来。我使用了字符串替换,将NSString的内容转换为摩尔斯电码 // some of the code : str = [str stringByReplacingOccurrencesOfString:@"5" withString:n5]; str = [str stringByReplacingOccurrencesOfString:@"6" withString:n6]; str
// 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;
我发现了一个脚本,可以使用NSTimer打开和关闭iPhone的手电筒,时间间隔可以调整。但我不知道如何添加两个不同的间隔,一个是点间隔,一个是莫尔斯破折号间隔
- (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];
}
}
我应该使用两个计时器还是可以使用一个不同间隔的计时器?我应该将字符串的内容放入数组中吗?
我是Obj-C的新手。您可以尝试在后台树上按顺序运行代码,并根据需要休眠它多长时间。编写和维护代码要比使用一堆计时器容易得多
// 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];
// ...
}
我将尝试创建一个递归函数:
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];
}