Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have full rotation of a circular object using uiview animation

I am trying to loop over partial circles to achieve the animation but it only turns 180 degrees, and then starts again from 0 degrees to 180 degrees. So there is an abrupt jump from 180 degrees to 360 degrees. How can I have my circular image object rotate continuously without any jumps? Here is my current code:

    UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear|UIViewAnimationOptionRepeat;

    [UIView animateWithDuration:ROTATE_ANIMATION_DURATION/2 delay:0 options:options 
       animations:^{
           view.transform = CGAffineTransformRotate(transform, M_PI);}//1st step of animation finished
       completion:^(BOOL finished) {
             [UIView animateWithDuration:ROTATE_ANIMATION_DURATION/2 delay:0 options:options 
                animations:^{
                    view.transform = CGAffineTransformRotate(transform, M_PI);} //2nd step of animation finished
                completion:^(BOOL finished) {nil;
                }];
like image 964
user1620383 Avatar asked Dec 03 '22 02:12

user1620383


2 Answers

Spin UI Object, animation rotate 360 Degree
you’ll need to add the QuartzCore Framework to your project and include the

#import QuartzCore/QuartzCore.h

Add animation for object:

 CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 6;
    fullRotation.repeatCount = 1e100f;
    [myview.layer addAnimation:fullRotation forKey:@"360"];
like image 154
Hiren Avatar answered Apr 27 '23 13:04

Hiren


I found some old code that I use to rotate a view 360 degrees, I hope it helps. You'll need to import QuartzCore.

CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.zRad"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];

rotationAnimation.duration = 3;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = INFINITY;
rotationAnimation.timingFunction = 
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

[viewToAnimate.layer addAnimation:rotationAnimation forKey:@"transform.rotation.zRad"];
like image 42
ggfela Avatar answered Apr 27 '23 13:04

ggfela