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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:19:15+00:00 2026-06-16T03:19:15+00:00

I am trying to achieve an animation that when you hold down a button

  • 0

I am trying to achieve an animation that when you hold down a button it animates a block down, and when you release, it animates it back up to the original position, but I cannot obtain the current position of the animating block no matter what. Here is my code:

-(IBAction)moveDown:(id)sender{

   CGRect position = [[container.layer presentationLayer] frame];

    [movePath moveToPoint:CGPointMake(container.frame.origin.x, position.y)];

    [movePath addLineToPoint:CGPointMake(container.frame.origin.x, 310)];

    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    moveAnim.path = movePath.CGPath;
    moveAnim.removedOnCompletion = NO;
    moveAnim.fillMode = kCAFillModeForwards;


    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects:moveAnim, nil];
    animGroup.duration = 2.0;
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;

    [container.layer addAnimation:animGroup forKey:nil];
}
-(IBAction)moveUp:(id)sender{
     CGRect position = [[container.layer presentationLayer] frame];

    UIBezierPath *movePath = [UIBezierPath bezierPath];

    [movePath moveToPoint:CGPointMake(container.frame.origin.x, position.y)];

    [movePath addLineToPoint:CGPointMake(container.frame.origin.x, 115)];

    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    moveAnim.path = movePath.CGPath;
    moveAnim.removedOnCompletion = NO;
    moveAnim.fillMode = kCAFillModeForwards;

    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects:moveAnim, nil];
    animGroup.duration = 2.0;
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;

    [container.layer addAnimation:animGroup forKey:nil];

}

But the line

   CGRect position = [[container.layer presentationLayer] frame];

is only returning the destination position not the current position. I need to basically give me the current position of the container thats animating once I release the button, so I can perform the next animation. What I have now does not work.

  • 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-16T03:19:16+00:00Added an answer on June 16, 2026 at 3:19 am

    I haven’t analyzed your code enough to be 100% sure why [[container.layer presentationLayer] frame] might not return what you expect. But I see several problems.

    One obvious problem is that moveDown: doesn’t declare movePath. If movePath is an instance variable, you probably want to clear it or create a new instance each time moveDown: is called, but I don’t see you doing that.

    A less obvious problem is that (judging from your use of removedOnCompletion and fillMode, in spite of your use of presentationLayer) you apparently don’t understand how Core Animation works. This turns out to be surprisingly common, so forgive me if I’m wrong. Anyway, read on, because I will explain how Core Animation works and then how to fix your problem.

    In Core Animation, the layer object you normally work with is a model layer. When you attach an animation to a layer, Core Animation creates a copy of the model layer, called the presentation layer, and the animation changes the properties of the presentation layer over time. An animation never changes the properties of the model layer.

    When the animation ends, and (by default) is removed, the presentation layer is destroyed and the values of the model layer’s properties take effect again. So the layer on screen appears to “snap back” to its original position/color/whatever.

    A common, but wrong way to fix this is to set the animation’s removedOnCompletion to NO and its fillMode to kCAFillModeForwards. When you do this, the presentation layer hangs around, so there’s no “snap back” on screen. The problem is that now you have the presentation layer hanging around with different values than the model layer. If you ask the model layer (or the view that owns it) for the value of the animated property, you’ll get a value that’s different than what’s on screen. And if you try to animate the property again, the animation will probably start from the wrong place.

    To animate a layer property and make it “stick”, you need to change the model layer’s property value, and then apply the animation. That way, when the animation is removed, and the presentation layer goes away, the layer on screen will look exactly the same, because the model layer has the same property values as its presentation layer had when the animation ended.

    Now, I don’t know why you’re using a keyframe to animate straight-line motion, or why you’re using an animation group. Neither seems necessary here. And your two methods are virtually identical, so let’s factor out the common code:

    - (void)animateLayer:(CALayer *)layer toY:(CGFloat)y {
        CGPoint fromValue = [layer.presentationLayer position];
        CGPoint toValue = CGPointMake(fromValue.x, y);
    
        layer.position = toValue;
    
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
        animation.fromValue = [NSValue valueWithCGPoint:fromValue];
        animation.toValue = [NSValue valueWithCGPoint:toValue];
        animation.duration = 2;
        [layer addAnimation:animation forKey:animation.keyPath];
    }
    

    Notice that we’re giving the animation a key when I add it to the layer. Since we use the same key every time, each new animation will replace (remove) the prior animation if the prior animation hasn’t finished yet.

    Of course, as soon as you play with this, you’ll find that if you moveUp: when the moveDown: is only half finished, the moveUp: animation will appear to be at half speed because it still has a duration of 2 seconds but only half as far to travel. We should really compute the duration based on the distance to be travelled:

    - (void)animateLayer:(CALayer *)layer toY:(CGFloat)y withBaseY:(CGFloat)baseY {
        CGPoint fromValue = [layer.presentationLayer position];
        CGPoint toValue = CGPointMake(fromValue.x, y);
    
        layer.position = toValue;
    
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
        animation.fromValue = [NSValue valueWithCGPoint:fromValue];
        animation.toValue = [NSValue valueWithCGPoint:toValue];
        animation.duration = 2.0 * (toValue.y - fromValue.y) / (y - baseY);
        [layer addAnimation:animation forKey:animation.keyPath];
    }
    

    If you really need it to be a keypath animation in an animation group, your question should show us why you need those things. Anyway, it works with those things too:

    - (void)animateLayer:(CALayer *)layer toY:(CGFloat)y withBaseY:(CGFloat)baseY {
        CGPoint fromValue = [layer.presentationLayer position];
        CGPoint toValue = CGPointMake(fromValue.x, y);
    
        layer.position = toValue;
    
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:fromValue];
        [path addLineToPoint:toValue];
    
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        animation.path = path.CGPath;
        animation.duration =  2.0 * (toValue.y - fromValue.y) / (y - baseY);
    
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.duration = animation.duration;
        group.animations = @[animation];
        [layer addAnimation:group forKey:animation.keyPath];
    }
    

    You can find the full code for my test program in this gist. Just create a new Single View Application project and replace the contents of ViewController.m with the contents of the gist.

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

Sidebar

Related Questions

I have been continuously trying to customize OpenAphide project but Unable to achieve that,Actually
im trying to achieve something but i dont really know how I have set
I am trying to achieve something in MySQL but even with all the answers
I am trying to achieve dynamic META information so that it will allow META
I'm playing around with a little jquery animation and trying to achieve a very
I am trying to implement a shake animation using jQuery. The idea is that
I'm trying to create an animation that allows a user to pull out a
I have trying to achieve SVG element's animation while adding dynamic DOMs for its
I'm trying to build an animation effect where pressing a button at the bottom
I'm trying to create a transition/animation on a UIView that looks something like the

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.