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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:49:20+00:00 2026-06-02T01:49:20+00:00

I have a custom button which is my own subclass of UIButton . It

  • 0

I have a custom button which is my own subclass of UIButton. It looks like a circled arrow, starting at some angle startAngle end finishing at some endAngle=startAngle+1.5*M_PI.
startAngle is a button’s property which is then used in its drawRect: method.
I want to make this arrow to continuously rotate by 2Pi around its center when this button is pressed. So I thought that I can easily use [UIView beginAnimations: context:] but apparently it can’t be used as it doesn’t allow to animate custom properties. CoreAnimation also doesn’t suite as it only animates the CALayer properties.

So what is the easiest way to implement an animation of a custom property of UIView subclass in iOS?
Or maybe I missed something and it is possible with already mentioned techniques?

Thank you.

  • 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-02T01:49:22+00:00Added an answer on June 2, 2026 at 1:49 am

    Thanks to Jenox I have updated animation code using CADisplayLink which seems to be really more correct solution than NSTimer. So I show the correct implementation with CADisplayLink now. It is very close to the previous one, but even a bit simpler.

    We add the QuartzCore framework to our project.
    Then we put the following lines in the header file of our class:

    CADisplayLink* timer;
    Float32 animationDuration;  // Duration of one animation loop
    Float32 initAngle;  // Represents the initial angle 
    Float32 angle;  // Angle used for drawing
    CFTimeInterval startFrame;  // Timestamp of the animation start
    
    -(void)startAnimation;  // Method to start the animation
    -(void)animate;  // Method for updating the property or stopping the animation
    

    Now in implementation file we set the values for duration of the animation and the other initial values:

    initAngle=0.75*M_PI;
    angle=initAngle;
    animationDuration=1.5f;  // Arrow makes full 360° in 1.5 seconds
    startFrame=0;  // The animation hasn't been played yet
    

    To start the animation we need to create the CADisplayLink instance which will call method animate and add it to main RunLoop of our application:

    -(void)startAnimation
    {
       timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(animate)];
       [timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    }
    

    This timer will call animate method every runLoop of the application.

    So now comes the implementation of our method for updating the property after each loop:

    -(void)animate
    {
       if(startFrame==0) {
          startFrame=timer.timestamp;  // Setting timestamp of start of animation to current moment
          return;  // Exiting till the next run loop
       } 
       CFTimeInterval elapsedTime = timer.timestamp-startFrame;  // Time that has elapsed from start of animation
       Float32 timeProgress = elapsedTime/animationDuration;  // Determine the fraction of full animation which should be shown
       Float32 animProgress = timingFunction(timeProgress); // The current progress of animation
       angle=initAngle+animProgress*2.f*M_PI;  // Setting angle to new value with added rotation corresponding to current animation progress
       if (timeProgress>=1.f) 
       {  // Stopping animation
          angle=initAngle; // Setting angle to initial value to exclude rounding effects
          [timer invalidate];  // Stopping the timer
          startFrame=0;  // Resetting time of start of animation
       }
       [self setNeedsDisplay];  // Redrawing with updated angle value
    }
    

    So unlike case with NSTimer we now don’t need to calculate the time interval at which to update the angle property and redraw the button. We now only need to count how much time has passed from the start of animation and set the property to value which corresponds to this progress.

    And I must admit that animation works a bit more smoothly than in case of NSTimer.
    By default, CADisplayLink calls the animate method each run loop. When I calculated the frame rate, it was 120 fps. I think that it is not very efficient so I have decreased the frame rate to just 22 fps by changing the frameInterval property of CADisplayLink before adding it to mainRunLoop:

    timer.frameInterval=3;
    

    It means that it will call the animate method at first run loop, then do nothing next 3 loops, and call on the 4-th, and so on. That’s why frameInterval can be only integer.

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

Sidebar

Related Questions

I have a custom button which inherits from UIButton. I'm handling the TouchUpInside event
I have a custom UITableViewCell which contains several UIButtons. Each button's frame position is
I have to create a custom compound button bar control in Android like the
I have a custom button in my application on click of which I present
I have made a custom suface button according this example: <Button> <Button.Template> <ControlTemplate TargetType=Button>
I have custom server control contains templatefield when an out button clicked the field
I'm making a custom button (Winforms Control Library) and have the code below so
Check out this jsbin . I have a form with a custom button that
I have a custom ButtonUI class that paints the button. Before drawing the text,
I have a custom control that displays a tryAgain button when the control is

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.