Armand
Armand

Reputation: 435

iOS - Text displayed on animation

I am animating a set of frames in iOS and would like to have a text displayed at the end of it. At this point I'm only able to animate the images. The text I'm trying to display on it using CATextLayer does not show up. I simply don't know how to make the layer a subview of the view. Also the initWithFrame method I was trying to use does not seem to work with UIViewController. Could anyone help out?

1) Here's the animation code that works perfectly:

//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) Here's how I'm trying to display a layer containing text in it. As you can see I'm not even at the point to display it at the end. Still trying to have it there in the first place and already running into issues:

// 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];

Upvotes: 1

Views: 337

Answers (1)

Chris Loonam
Chris Loonam

Reputation: 5745

replace

[[self layer] addSublayer:boxLayer];

with

[self.view.layer addSublayer:boxLayer];

or

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

they're both the same

Edit

after testing, it worked perfect for me

Upvotes: 1

Related Questions