iOS developer
iOS developer

Reputation: 68

How to programmatically rotate button by 360 Degrees in iPhone?

how the button can be rotated by 360 degree for duration of time 30 sec and after that the button stop rotation.

Upvotes: 2

Views: 4782

Answers (4)

Abelardo
Abelardo

Reputation: 101

Well I just used

Being self.buttonImageView the ImageView you need to rotate.

[UIView animateKeyframesWithDuration:0.5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{

    [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.25 animations:^{
        self.buttonImageView.transform= CGAffineTransformMakeRotation(M_PI);
    }];

    [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{
        self.buttonImageView.transform= CGAffineTransformMakeRotation(-2* M_PI);
    }];


} completion:^(BOOL finished) {

    [self.map setCenterCoordinate:self.map.userLocation.location.coordinate animated:YES];
    self.buttonImageView.transform= CGAffineTransformMakeRotation(2* M_PI);
}];

Upvotes: 0

Asif Mujteba
Asif Mujteba

Reputation: 4656

CATransform3D myRotationTransform = CATransform3DRotate(Yourbutton.layer.transform, -1, 0.0, 0.0, 1.0);
Yourbutton.layer.transform = myRotationTransform;

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: -1];
animation.duration = 30;
animation.repeatCount = 1;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[Yourbutton.layer addAnimation:animation forKey:@"MyAnimation"];

Should work as needed! don't forget to include quartz.framework!

Upvotes: 0

David Rönnqvist
David Rönnqvist

Reputation: 56635

A 360 rotation animation is only a few lines of code with Core Animation.

CABasicAnimation *rotate = 
    [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.byValue = @(M_PI*2); // Change to - angle for counter clockwise rotation
rotate.duration = 30.0; 

[yourButton.layer addAnimation:rotate 
                        forKey:@"myRotationAnimation"];

By using the byValue property you are doing a relative rotation of 360 degrees to whatever rotation was there before (compared to explicitly specifying the from and to values). This means that the above code will rotate the button 360 degrees even if it is already rotated. All the answers that explicitly specify an end transform are assuming that the button isn't already rotated.

The above example is as small as possible to do just what you asked for ("be rotated by 360 degree for duration of time 30 sec"). If you want to have more control you can optionally make the animation start and/or stop slowly by specifying a timing function

rotate.timingFunction = 
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 

If you haven't already added QuarzCore.framework to your project you will need to do so. Also #import <QuartzCore/QuartzCore.h> in the top of your source file.

Upvotes: 8

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

[UIView animateWithDuration:.30f animations:^{
    btnGallery.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI);
}];

Upvotes: -1

Related Questions