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 8691561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:08:34+00:00 2026-06-13T00:08:34+00:00

I am learning Core Animation and trying out sample examples. When I use the

  • 0

I am learning Core Animation and trying out sample examples.

When I use the following code, the animation duration works

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

//Modifying base layer
self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
self.view.layer.cornerRadius = 20.0;
self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);

//Adding layer
mylayer=[CALayer layer]; //mylayer declared in .h file
mylayer.bounds=CGRectMake(0, 0, 100, 100);
mylayer.position=CGPointMake(100, 100); //In parent coordinate
mylayer.backgroundColor=[UIColor redColor].CGColor;
mylayer.contents=(id) [UIImage imageNamed:@"glasses"].CGImage;

[self.view.layer addSublayer:mylayer];
}


- (IBAction)Animate //Simple UIButton
{
[CATransaction begin];

// change the animation duration to 2 seconds
[CATransaction setValue:[NSNumber numberWithFloat:2.0f] forKey:kCATransactionAnimationDuration];

mylayer.position=CGPointMake(200.0,200.0);
mylayer.zPosition=50.0;
mylayer.opacity=0.5;

[CATransaction commit];
}
@end

On the other hand, if I lumped the Animate method code at the bottom of the ViewDidLoad button so that it happens without pressing any buttons, the animation duration is not respected. I just see the final result without any animation.

Any thoughts?

Thanks
KMB

  • 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-06-13T00:08:35+00:00Added an answer on June 13, 2026 at 12:08 am

    Here’s the information you’re missing: there are two layer hierarchies in your app. There’s the model layer hierarchy, which you normally operate on. Then there’s the presentation layer hierarchy, which reflects what’s on the screen. Take a look at “Layer Trees Reflect Different Aspects of the Animation State” in the Core Animation Programming Guide for a bit more information, or (highly recommended) watch the Core Animation Essentials video from WWDC 2011.

    All of the code you wrote operates on the model layer (as it should).

    The system adds implicit animations when it copies a changed animatable property value from a model layer to the corresponding presentation layer.

    Only model layers that are in a UIWindow‘s view hierarchy get presentation layers. The system sends you viewDidLoad before it has added self.view to the window, so there are no presentation layers for self.view or your custom layer yet when viewDidLoad is running.

    So one thing you need to do is change the property later, after the view and the layer have been added to the window and the system has created the presentation layers. The viewDidAppear: is late enough.

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //Modifying base layer
        self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
        self.view.layer.cornerRadius = 20.0;
        self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);
    
        // Adding layer
        mylayer = [CALayer layer]; //mylayer declared in .h file
        mylayer.bounds = CGRectMake(0, 0, 100, 100);
        mylayer.position = CGPointMake(100, 100); //In parent coordinate
        mylayer.backgroundColor = [UIColor redColor].CGColor;
        mylayer.contents = (id)[UIImage imageNamed:@"glasses"].CGImage;    
        [self.view.layer addSublayer:mylayer];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        [CATransaction begin]; {
            [CATransaction setAnimationDuration:2];
            mylayer.position=CGPointMake(200.0,200.0);
            mylayer.zPosition=50.0;
            mylayer.opacity=0.5;
        } [CATransaction commit];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just learning Core Data on iPhone and the tutorial I am following
I am learning to use Core Data. I have created a data model etc
I am using Apple's CoreDataBooks sample project as a learning aid for core data.
In learning Core Animation, I learned very quickly that if you don't do it
I'm learning Core Data with the help of the book. There is a code:
I'm currently learning to use Core Data on iOS , in my test application
I've just started learning to use Core Data. On my first attempt to add
I've been learning how to use Core Data in xcode for a couple of
I am learning Core Data so I was confused about the following. I need
I am learning core data concept and i want to use this in my

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.