iOS - 动画上显示的文本

标签 ios animation text layer catextlayer

我正在为 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/

相关文章:

iphone - 以编程方式关闭 UIAlertView

ios - 理解 iOS 编程中的委托(delegate)

ios - 在 CoreData 中存储日期 - 将日期字符串转换为 NSTimeInterval?

c++ - D3D9 2D 动画中的奇怪之处——高 FPS 但仍然不流畅

jquery - 有没有一种方法可以在 jQuery 中对元素进行动画处理,并为高度变化的每个像素触发一个函数?

c# - C#/dot-NET 的字体渲染库?

ios - 属性 setter responseToSelector

jquery动画动态高度div

python - 遵循命名约定从每个文本文件中提取值 - 如何?

python - 语音到ext转换