Search code examples
iphonecocoa-touchuikitcore-animation

replaceSublayer example


I have the following code and I can't seem to get it to work.

First I create a view with a layer.

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];

Next I need to replace that layer. I have tried the following and it fails because I don't know how to access that layer 0. At this point I am in a different part of the program so I cannot just call gradient. I need to extricate it from view somehow.

[view.layer replaceSublayer:0 with:newgradient];

Apparently the 0 is supposed to be Old layer but I don't know how to access it.

Thanks.


Solution

  • I think you should be able to use this:

    [view.layer replaceSublayer:[[view.layer sublayers] objectAtIndex:0]
                with:newGradient];
    

    I'm away from my Xcode development environment and can't test this at the moment though.