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 online now   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 online now   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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
CALayer to pdf MrFusion Mac Programming 1 Aug 6, 2011 03:39 PM
Animation Desk for iPhone - Anyone can be an animator. Any idea matters. (New Video) swopes iPhone and iPod touch Apps 4 Jul 24, 2011 09:59 AM
zoom animation using animation with block based methods ashwinr87 iPhone/iPad Programming 4 Apr 20, 2011 09:24 AM
How does one make a UIImage Animation stop on an image. nickculbertson iPhone/iPad Programming 3 Nov 20, 2010 07:50 AM
How to write a jQuery animate in this situation? youlichika Web Design and Development 1 Nov 19, 2010 03:47 PM


All times are GMT -5. The time now is 01:27 PM.

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

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