Animating a CALayer sublayer

Discussion in 'iOS Programming' started by yaris20, Nov 17, 2010.

  1. macrumors newbie

    Joined:
    Nov 17, 2010
    #1
    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.
     
  2. macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #2
    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?
     
  3. thread starter macrumors newbie

    Joined:
    Nov 17, 2010
    #3
    Thanks for your reply.
    My sublayer is released later in the dealloc method.

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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    How does your dealloc method have a reference to your sublayer? You've created it as a local variable.
     
  5. thread starter macrumors newbie

    Joined:
    Nov 17, 2010
    #5
    In fact my sublayer is an instance variable of a UIView class. I didn't want to write the whole code.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    And you're sure the sublayer's frame is set as needed?
     
  7. thread starter macrumors newbie

    Joined:
    Nov 17, 2010
    #7
    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).
     
  8. macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #8
    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.
     
  9. thread starter macrumors newbie

    Joined:
    Nov 17, 2010
    #9
    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.
     

Share This Page