【问题标题】:iOS - Text displayed on animationiOS - 动画上显示的文本
【发布时间】:2013-01-27 22:05:41
【问题描述】:

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

【问题讨论】:

    标签: ios animation text layer catextlayer


    【解决方案1】:

    替换

    [[self layer] addSublayer:boxLayer];
    

    [self.view.layer addSublayer:boxLayer];
    

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

    他们都是一样的

    编辑

    经过测试,非常适合我

    【讨论】:

    • 太棒了!文本得到显示。谢谢。现在它一直显示。你知道我怎样才能让它出现在动画的结尾吗?我应该使用 NSTimer 吗?
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    相关资源
    最近更新 更多