我正在使用 NSTimer 将文本写入 CATextLayer,但文本仅显示一瞬间。
我这样设置第一个计时器:
-(void) startSentanceAnimation{
float firstTime = [[sentanceArray objectAtIndex:0][@"time"]floatValue];
[NSTimer scheduledTimerWithTimeInterval:firstTime target:self selector:@selector(timedSentances:) userInfo:sentanceArray repeats:NO];
}
它调用此方法来更改文本,然后创建一个新计时器。
-(void)timedSentances:(NSTimer *)timer{
NSArray *userInfo = timer.userInfo;
timer = nil;
NSString *sentance = [userInfo objectAtIndex:sentanceCount][@"sentance"];
[self nextLine:sentance];
//textLayer.string = sentance;
float intervalLength = [[userInfo objectAtIndex:sentanceCount][@"time"]floatValue];
[NSTimer scheduledTimerWithTimeInterval:intervalLength target:self selector:@selector(timedSentances:) userInfo:userInfo repeats:NO];
sentanceCount++;
}
代码运行没有错误,文本在适当的时间显示,但文本只在屏幕上闪烁一瞬间,并且不会在方法调用之间持续存在。是因为我没有保留对计时器的引用吗?
应使用 nextLine 方法设置文本,如下所示:
-(void)nextLine:(NSString *)st{
textLayer.string = st;
}
TextLayer 是一个 CATextLayer,并在接口中声明为变量,如下所示:
@interface ICIRecordDetail(){
CATextLayer *textLayer;
}
它在 viewDidLoad 方法中被实例化。
我也试过这样设置文本:
[textLayer setText:sentance];
但同样的事情也会发生。句子仅在计时器触发时出现,然后再次消失。我想知道是不是因为它在 CATextLayer 中?对IOS非常陌生,所以不知所措。任何帮助都会很棒。谢谢