Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 17, 2010, 01:37 PM   #1
yaris20
macrumors newbie
 
Join Date: Nov 2010
Animating a CALayer sublayer

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.
yaris20 is offline   0 Reply With Quote
Old Nov 17, 2010, 02:21 PM   #2
Spike099
macrumors regular
 
Join Date: Feb 2007
Location: 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?
__________________
MacBook 4GBRam 320GBHDD
iMac 20" 4GBRam 1TBHDD
iPhone 16GB
Cocoa Developer
Spike099 is offline   0 Reply With Quote
Old Nov 18, 2010, 12:53 PM   #3
yaris20
Thread Starter
macrumors newbie
 
Join Date: Nov 2010
Thanks for your reply.
Quote:
Originally Posted by Spike099 View Post
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.

Quote:
Originally Posted by Spike099 View Post
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 is offline   0 Reply With Quote
Old Nov 18, 2010, 12:57 PM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by yaris20 View Post
My sublayer is released later in the dealloc method.
How does your dealloc method have a reference to your sublayer? You've created it as a local variable.
__________________
dejo is offline   0 Reply With Quote
Old Nov 18, 2010, 01:00 PM   #5
yaris20
Thread Starter
macrumors newbie
 
Join Date: Nov 2010
Quote:
Originally Posted by dejo View Post
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.
yaris20 is offline   0 Reply With Quote
Old Nov 18, 2010, 02:14 PM   #6
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
And you're sure the sublayer's frame is set as needed?
__________________
dejo is offline   0 Reply With Quote
Old Nov 18, 2010, 03:27 PM   #7
yaris20
Thread Starter
macrumors newbie
 
Join Date: Nov 2010
Quote:
Originally Posted by dejo View Post
And you're sure the sublayer's frame is set as needed?
Yes. My sublayer's frame rectangle is (0.0f, 0.0f, 320.0f, 240.0f) its superlayer's is (0.0f, 0.0f, 320.0f, 480.0f).
yaris20 is offline   0 Reply With Quote
Old Nov 23, 2010, 02:42 PM   #8
Spike099
macrumors regular
 
Join Date: Feb 2007
Location: Canada
It sound like the layer isn't actually added as a sublayer.

Do this:

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

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

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.
__________________
MacBook 4GBRam 320GBHDD
iMac 20" 4GBRam 1TBHDD
iPhone 16GB
Cocoa Developer
Spike099 is offline   0 Reply With Quote
Old Nov 24, 2010, 11:18 AM   #9
yaris20
Thread Starter
macrumors newbie
 
Join Date: Nov 2010
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.
yaris20 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
CALayer backed anchorPoint property is not .5, .5 by default? Duncan C Mac Programming 0 Jun 7, 2013 07:12 PM
CALayer approach in jre7 LeanneAttard Mac Programming 1 May 28, 2013 05:06 AM
Icons not animating. Running 10.8.0 Spontida OS X 1 Feb 28, 2013 06:10 PM
Difference between UIView's drawRect and CALayer? Apple souce iPhone/iPad Programming 2 Nov 2, 2012 03:05 PM
what is the easiest way of animating some still video? jojoba Mac Applications and Mac App Store 0 Oct 29, 2012 05:32 AM

Forum Jump

All times are GMT -5. The time now is 02:52 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC