我正在编写一个使用动画文本的可可控件的应用程序,
https://www.cocoacontrols.com/controls/marqueelabel
它使用CoreText
和QuartzCore
。
这是一个非常棒的控制,但我遇到了麻烦。当实例化我使用控件创建的动画标签时,它会立即开始滚动,但是我发现当我使用NSTimer
它创建动画标签时没有动画。我在用
- (void)viewDidLoad
{
[super viewDidLoad];
....
[self.view addSubview:continuousLabel1];
[NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:@selector(updatePrice)
userInfo:nil repeats:YES];
}
调用创建动画标签的方法,当我直接调用该方法时,标签创建和动画,但是当使用上述预定计时器调用时,新标签仅在第一次调用时创建动画,而不是在计时器上重复调用该方法. 我检查了我的方法是否在主线程/主运行循环上被调用,有什么想法吗?
为清楚起见,工作流程是:
计时器调用方法 --> 标签 1 已创建并开始进行动画处理。
迟到 5 秒...
计时器再次调用方法 --> 创建了标签 2 但是没有按预期开始动画。
编辑:
- (void)updatePrice
{
MarqueeLabel *continuousLabel2 = [[MarqueeLabel alloc] initWithFrame:CGRectMake(10, 230, self.view.frame.size.width-20, 20) rate:100.0f andFadeLength:10.0f];
continuousLabel2.tag = 101;
continuousLabel2.marqueeType = MLContinuous;
continuousLabel2.animationCurve = UIViewAnimationOptionCurveLinear;
continuousLabel2.continuousMarqueeExtraBuffer = 50.0f;
continuousLabel2.numberOfLines = 1;
continuousLabel2.opaque = NO;
continuousLabel2.enabled = YES;
continuousLabel2.shadowOffset = CGSizeMake(0.0, -1.0);
continuousLabel2.textAlignment = NSTextAlignmentLeft;
continuousLabel2.textColor = [UIColor colorWithRed:0.234 green:0.234 blue:0.234 alpha:1.000];
continuousLabel2.backgroundColor = [UIColor clearColor];
continuousLabel2.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.000];
continuousLabel2.text = @"This is another long label that doesn't scroll continuously with a custom space between labels! You can also tap it to pause and unpause it!";
[self.view addSubview:continuousLabel2];
if( [NSThread isMainThread])
{
NSLog(@"In Main Thread");
}
if ([NSRunLoop currentRunLoop] == [NSRunLoop mainRunLoop]) {
NSLog(@"In Main Loop");
}
}