我正在写一个应用程序使用cocoa控件动画文本,
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秒后…
Timer再次调用方法--> Label 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");
}
}
问题是你每5秒调用updatePrice
,在这个方法中,你正在创建一个新的标签,那就是堆叠,并注意到它每5秒变得越来越暗。
如果你检查标签是滚动的
解决方案:
如果您想要多个标签实例,请更改其坐标。
如果你想要单个实例,那么比较check并只创建一次。
你应该阅读README文件中的重要动画注意事项。
我的猜测是,你将需要调用controllerLabelsShouldAnimate:
传递你的当前视图控制器动画那些标签添加后,视图控制器已经显示。