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

  • SEARCH
  • Home
  • 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 6682481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:42:41+00:00 2026-05-26T04:42:41+00:00

I have an app with CALayers that move from one side of the screen

  • 0

I have an app with CALayers that move from one side of the screen to the other. I am trying to get the timing right so that they all take the same time to pass from the first side to an arbitrary delimiter I’ve decided on (near the other side), then continue their run to disappear from the screen.

Here’s an example of what I mean:

visual description of my problem

Both boxes should move at the same speed (the animation has linear timing), so when the small box hits the delimiter, the small gap between the two should be the same.

However, I rather see this:

visual description of what happens

Expressed with words, it seems that my large box is moving faster than the small box.

Here’s the relevant part of my code. I decide a speed in pixels per second, then set the duration of the animation considering the number of pixels boxes have to travel.

const CGRect viewBounds = CGRectMake(0,0, 500, 400);
const CGFloat distanceToCross = viewBounds.size.width - delimiterX;
const CFTimeInterval timeToCross = 5;
const CGFloat pixelsPerSecond = distanceToCross / timeToCross;

const CGFloat y = /* y coordinate of the box */;
CALayer* box = [CALayer layer];
// the layer should lie outside the viewport at first
box.frame = CGRectMake(viewBounds.size.width, y, /* size here */);
box.anchorPoint = CGPointMake(0, 0.5);
// snip

[CATransaction begin];
CATransaction.animationTimingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CATransaction.animationDuration = (viewBounds.size.width + box.frame.size.width) / pixelsPerSecond;
// and the layer should cross the viewport to outside of it, too
box.position = CGPointMake(-box.frame.size.width, y);
[CATransaction commit];

This is not shown in my graphical example, but boxes of the same size all go at the same speed, so obviously I’m doing something wrong with my time calculation. What am I missing?

  • 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-26T04:42:42+00:00Added an answer on May 26, 2026 at 4:42 am

    CALayer.position, by default, indicates the center point of the box. As such, your ending frame for each box is actually past the point you want, and the amount it goes over is based on the size of the box. This is going to throw off your calculation since your duration is based on the wrong distance. I’m surprised you haven’t noticed your boxes drifting slightly downwards as they move, since the y coordinate is used for both frame.origin.y and position.y as well.

    A simple fix is to take the midpoint of the frame. You can do this by doing something like

    CGFrame newFrame = (CGRect){{-box.frame.size.width, y}, box.frame.size};
    box.position = CGPointMake(CGRectGetMidX(newFrame), CGRectGetMidY(newFrame));
    

    Or to make it resilient in the face of anchorPoint changes, you could use something like

    box.position = CGPointMake(-box.frame.size.width + box.frame.size.width*box.anchorPoint.x, y + box.frame.size.height*box.anchorPoint.y);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app have a background image that fills the screen. I'd like to display
We have App A as main app. Now we build from it App B
I have app in which i have recorded sound files i want that i
have an app that finds your GPS location successfully, but I need to be
Have an app that has listings - think classified ads - and each listing
I have app that works perfectly fine on my Phone (desire HD). When I
I have app that sometimes crashes in navigating through Navigation bar and in console
I have an iPad app that has draggable UIViews contained in a larger parent
I have an app that uses OpenGL-ES and an EAGLContext within a UIView -
I have a Mac app that's using IKImageBrowserView. I've subclassed IKImageBrowserView and I'm returning

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.