1

我正在为 iOS 中的一组帧设置动画,并希望在其末尾显示一个文本。在这一点上,我只能为图像设置动画。我尝试使用 CATextLayer 在其上显示的文本未显示。我根本不知道如何使图层成为视图的子视图。此外,我尝试使用的 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];
4

1 回答 1

1

代替

[[self layer] addSublayer:boxLayer];

[self.view.layer addSublayer:boxLayer];

或者

[[self.view layer] addSublayer:boxLayer];

他们都是一样的

编辑

经过测试,它对我来说很完美

于 2013-01-27T22:16:45.470 回答