Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yaris20

macrumors newbie
Original poster
Nov 17, 2010
5
0
Hi.

I'm trying to animate the sublayer of a UIView's layer with the following code:

Code:
CALayer *sublayer = [[CALayer alloc] init];
    [sublayer setFrame:myRect]; // myRect is a portion of the view's frame rectangle
    [self.layer addSublayer:sublayer];

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    anim.duration = 1.0f;
    anim.fromValue = [NSNumber numberWithFloat:1];
    anim.toValue = [NSNumber numberWithFloat:.5];
    anim.delegate = self;
    [sublayer addAnimation:anim forKey:@"animateOpacity"];

I don't understand why my sublayer doesn't animate since the code
Code:
[self.layer addAnimation:anim forKey:@"animateOpacity"];
does animate the view's main layer.

Thanks.
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
Memory leak at line 1. Use this instead unless your releasing it later on in your code.

CALayer *sublayer = [[[CALayer alloc] init] autorelease];

Is your sublayer visible? When it appears, does it appear at 1 or 0.5 opacity?
 

yaris20

macrumors newbie
Original poster
Nov 17, 2010
5
0
Thanks for your reply.
Memory leak at line 1. Use this instead unless your releasing it later on in your code.

CALayer *sublayer = [[[CALayer alloc] init] autorelease];

My sublayer is released later in the dealloc method.

Is your sublayer visible? When it appears, does it appear at 1 or 0.5 opacity?

My sublayer isn't visible even with the lines below :
Code:
[sublayer setHidden:FALSE];
[sublayer setNeedsDisplay];
 

yaris20

macrumors newbie
Original poster
Nov 17, 2010
5
0
How does your dealloc method have a reference to your sublayer? You've created it as a local variable.
In fact my sublayer is an instance variable of a UIView class. I didn't want to write the whole code.
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
It sound like the layer isn't actually added as a sublayer.

Do this:

Code:
[B]CALayer *parentLayer = self.layer;[/B]
[B]self.sublayer = [CALayer layer][/B]; // proper way to set your member var - assuming the setter retains
[sublayer setFrame:myRect];
[[B]parentLayer[/B] addSublayer:sublayer];

[B]// SET A BREAKPOINT HERE
// ensure parentLayer is a valid CALayer
// ensure sublayer.superlayer contains the address of parentLayer[/B]

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
anim.duration = 1.0f;
anim.fromValue = [NSNumber numberWithFloat:1];
anim.toValue = [NSNumber numberWithFloat:.5];
anim.delegate = self;
[sublayer addAnimation:anim forKey:@"animateOpacity"];

Let me know what happens.
 

yaris20

macrumors newbie
Original poster
Nov 17, 2010
5
0
I have just solved the problem: my sublayer wasn't visible because it wasn't initialized with any content. I added the line
Code:
sublayer.backgroundColor = [[UIColor redColor] CGColor];
before setting the animation and it worked.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.