Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:31:39+00:00 2026-05-15T22:31:39+00:00

The code below handles a stack of CALayers . Every time a new layer

  • 0

The code below handles a stack of CALayers. Every time a new layer is pushed onto the stack the function is locked, and all existing layers and moved down on the screen to make room for the new layer. Once the last layer is done animating the function is unlocked again so that new layers can be pushed.

My problem is that every time this code runs the initial animation on newLayer. In other words, rather than positioning the new layer at CGPointMake(0, 0-offset) and then animating it to CGPointMake(0, currentLayer.position.y + offset) it shows up at its final position instantly. Am I missing something? Thanks!

-(void)addNewLayerWithHeight:(float)layerHeight {
    if(!animationLocked) {
        animationLocked = YES;
        //Offset is the ammount that each existing layer will need to be moved down by
        int offset = layerHeight;

        //Create the new layer
        CALayer *newLayer = [CALayer layer];
        [newLayer setBounds:CGRectMake(0, 0, self.view.layer.bounds.size.width, layerHeight)];
        [newLayer setAnchorPoint:CGPointMake(0, 0)];
        [newLayer setPosition:CGPointMake(0, 0-offset)];
        [newLayer setBackgroundColor:[[UIColor redColor] CGColor]];

        //Add the new layer to the view's layer and to layerArray
        [self.view.layer addSublayer:newLayer];
        [layerArray insertObject:newLayer atIndex:0];

        //loop through all layers and move them to their new position...
        for(int i=0;i<[layerArray count]; i++) {
            CALayer *currentLayer = [layerArray objectAtIndex:i];

            CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
            [anim setValue:@"stackLayer" forKey:@"kind"];
            [anim setValue:[NSNumber numberWithInt:i] forKey:@"index"];
            [anim setDelegate:self];
            [anim setDuration:1.0];

            currentLayer.actions = [NSDictionary dictionaryWithObject:anim forKey:@"position"];
            currentLayer.position = CGPointMake(0, currentLayer.position.y + offset);
        }
    }
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    //Make sure the last layer finished animating...
    if([[anim valueForKey:@"kind"] isEqual:@"stackLayer"] && [[anim valueForKey:@"index"] isEqual:[NSNumber numberWithInt:[layerArray count]-1]]) {
        animationLocked = NO;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:31:40+00:00Added an answer on May 15, 2026 at 10:31 pm

    You’re pretty close. I would just change your code in the loop to this:

    for(int i=0;i<[layerArray count]; i++)
    {
      CALayer *currentLayer = [layerArray objectAtIndex:i];
    
      CGPoint endPoint = CGPointMake(0, currentLayer.position.y + offset);
      CGPoint currentPoint = [currentLayer position];
    
      CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
      [anim setFromValue:[NSValue valueWithCGPoint:currentPoint]];
      [anim setToValue:[NSValue valueWithCGPoint:endPoint]];
      [anim setDelegate:self];
      [anim setDuration:1.0];
    
      [anim setValue:@"stackLayer" forKey:@"kind"];
      [anim setValue:[NSNumber numberWithInt:i] forKey:@"index"];
    
      [currentLayer setPosition:endPoint];
      [currentLayer addAnimation:anim forKey:@"position"];
    }
    

    This will ensure that your layer animates from the current position to the offset position as well as setting the position for the layer so that it doesn’t revert back to its starting position when the animation completes–though you may not get it to work right if you do it all in the same run loop. You might want to set up the layers and add them when your view loads, and then animate them as a response to some other action or by calling -performSelector:withObject:afterDelay passing it some delay that will allow it a chance to get queued for a later run loop iteration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hide the default constructor using protected modifier. Read More May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer I would do the database transaction after the user is… May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer Something like this works fine. header("Pragma: public", true); header("Expires: 0");… May 16, 2026 at 8:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Recently someone on Stack Overflow told me that the code below does not leak,
I am using GetModuleHandle in my function. That function gets called every time I
i got this exception when i done the code given below.. Unable to evaluate
I have tried to simplify and annotate the code which is giving me a
I'm very new to android, in fact only started yesterday. I managed to get
I am updating an old piece of C++ code and am stuck on a
I am trying to build an error handler for my desktop application. The code
I've looked at a lot of posts on different forums where others have received
Since I published a large update to one of my apps I have received
My program does some network activity in a background thread. Before starting, it pops

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.