我正在为 iOS 中的一组帧设置动画,并希望在其末尾显示一个文本。在这一点上,我只能为图像制作动画。我尝试使用 CATextLayer 在其上显示的文本未显示。我根本不知道如何使图层成为 View 的 subview 。此外,我尝试使用的 initWithFrame 方法似乎不适用于 UIViewController。有人能帮忙吗?
1) 这是完美运行的动画代码:
//setup animation
NSArray *theFrames = [NSArray array];
theFrames = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"frame1.png"],
[UIImage imageNamed:@"frame2.png"],
[UIImage imageNamed:@"frame3.png"],
[UIImage imageNamed:@"frame4.png"],
nil];
sceneFrames.animationImages = theFrames;
sceneFrames.animationDuration = 1;
sceneFrames.animationRepeatCount = 1;
sceneFrames.image = [theFrames objectAtIndex:theFrames.count - 1];
[sceneFrames startAnimating];
2) 以下是我尝试显示其中包含文本的层的方法。如您所见,我什至还没到最后显示它的地步。仍然试图首先将它放在那里并且已经遇到问题:
// Create the new layer object
boxLayer = [[CATextLayer alloc] init];
// Give it a size
[boxLayer setBounds:CGRectMake(0.0, 0.0, 300.0, 85.0)];
// Give it a location
[boxLayer setPosition:CGPointMake(160.0, 350.0)];
// Make half-transparent red the background color for the layer
UIColor *reddish = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
// Get CGColor object with the same color values
CGColorRef cgReddish = [reddish CGColor];
[boxLayer setBackgroundColor:cgReddish];
// Make it a sublayer on the view's layer
// THIS LINE DOES NOT WORK AND GIVES ME HEADACHES. It does not work without initWithFrame, which does not seem to work with UIViewController...
[[self layer] addSublayer:boxLayer];
// Create string
NSString *text2 = @"You are me.";
// Set font
[boxLayer setFont:@"Times New Roman"];
// Set font size
[boxLayer setFontSize:20.0];
[boxLayer setAlignmentMode:kCAAlignmentCenter];
// Assign string to layer
[boxLayer setString:text2];
最佳答案
替换
[[self layer] addSublayer:boxLayer];
与
[self.view.layer addSublayer:boxLayer];
或
[[self.view layer] addSublayer:boxLayer];
它们是一样的
编辑
经过测试,它非常适合我
关于iOS - 动画上显示的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14552863/