Search code examples
iphoneuibuttoncore-animation

How to animate uibuttons in a circular path (Objective C)


float degrees = -30.0;
float radians = (degrees/180.0) * M_PI;

[UIView animateWithDuration:0.5 animations:^
 {
     bottomRoundedView.transform = CGAffineTransformMakeRotation(radians);
 }
 ];

Solution

  • Try below code,

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.repeatCount = INFINITY;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnimation.duration = 3.0;
    
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGRect circleContainer = CGRectMake(0, 0, 100, 100); 
    CGPathAddEllipseInRect(curvedPath, NULL, circleContainer);
    
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);
    
    [self.yourbutton.layer addAnimation:pathAnimation forKey:@"myCircleAnimation"];
    

    circleContainer is the frame for which you need to show the circular animation. pathAnimation.duration is to adjust the speed of the animation.

    if you want to stop the animation then call

    [self.yourbutton.layer removeAnimationForKey:@"myCircleAnimation"];