Create a CATextlayer that changes at an interval in an AVMutableVideoComposition
Asked Answered
I

1

6

I have an app that allows a user to record a video in a mutable composition. I'd like to set some text that will come up and then change at an interval that I set when the user plays it back after an export.

For example, if the first word is "dog", then i'd like to set it up so that "cat" replaces that string X seconds later, and then is replaced with another word X seconds later.

My video is exported from an AVMutableComposition using AVExportSession and my words will be added using a CATextlayer added to it like so:

//code to setup AVMutableComposition
...
//code to setup CATextLayer and AVMutableVideoComposition
    CALayer *animatedTitleLayer = [CALayer layer];
    CATextLayer *titleLayer = [[CATextLayer alloc] init];
    titleLayer.string = @"Text I want to change at an interval";
    titleLayer.alignmentMode = kCAAlignmentCenter;
    titleLayer.bounds = CGRectMake(150, 50, 124, 354);
    titleLayer.position = CGPointMake(120, 270);
    titleLayer.bounds = CGRectIntegral(CGRectMake(0, 0, 250, 150));
    titleLayer.opacity = 1;
    titleLayer.backgroundColor = [UIColor purpleColor].CGColor;   
    [animatedTitleLayer addSublayer:titleLayer];
    animatedTitleLayer.position = CGPointMake(40, 5);        
    CALayer *parentLayer = [CALayer layer];
    CALayer *videoLayer = [CALayer layer];
    parentLayer.frame = CGRectMake(0, 0, 320, 480);
    videoLayer.frame = CGRectMake(0, 0, 320, 480);
    [parentLayer addSublayer:videoLayer];
    [parentLayer addSublayer:animatedTitleLayer];   
    parentLayer.preferredTransform = rotationTransform;
    AVMutableVideoComposition *videoComposition;
    videoComposition = [AVMutableVideoComposition videoComposition];
    videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

//code to setup AVExportSession
...

My question is: how can I make the text change to at an interval that I set to strings that I designate?

Any suggestions would be greatly appreciated.

Intellectuality answered 14/6, 2012 at 2:42 Comment(1)
Got any help? figured out the answer?Shahaptian
U
0

If what you are trying to achieve is changing the text of the CATextLayer at runtime with an interval, then one of your option is to search that layer in the hierarchy and change its text on some sort of timer thread. Provided array of strings to be replaced, you can set the text of the layer once you can find it at runtime.

You can head start with solving your first problem at hand: Search the layer in the hierarchy

All the subclasses of CALayer defines a property for identification of the layer in the hierarchy that is name.

So what you can do is you can give a name to your CATextLayer. In you case its your titleLayer:

titleLayer.name = "changingTextLayer"

then you can search it in the hierarchy:

for (CALayer *layer in [superLayerOfMyLayer sublayers]) {
        if ([[layer name] isEqualToString:"changingTextLayer"]) {
            return layer;
        }
    }

and once you find it you can change the text:

titleLayer.string = @"Changed text";

all you have to do now is setup a timer and do this every second/your proffered time interval, until there are strings in the array.

You will have to cast that CALayer to a CATextLayer.

Good luck.

Undesigned answered 25/2, 2017 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.