UIView Infinite 360 degree rotation animation?
Found a method (I modified it a bit) that worked perfectly for me: iphone UIImageView rotation
#import <QuartzCore/QuartzCore.h>
- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:(CGFloat)rotations repeat:(float)repeat {
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = repeat ? HUGE_VALF : 0;
[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
Rotate a view 360 degrees infinitely
Substitute pi * with /, delete repeat, add completion and recall the function like this:
private func rotateImageView() {
UIView.animate(withDuration: 3, delay: 0, options: .curveLinear, animations: {
self.vinylView.transform = self.vinylView.transform.rotated(by: .pi / 2)
}) { (finished) in
if finished {
self.rotateImageView()
}
}
}
Rotate UIView 360° forever
Use CABasicAnimation, not view animation, and rotate additively, not to an absolute value. It will also help if you rotate 180 degrees additively forever as this is easier to express.
Rotate a view for 360 degrees indefinitely in Swift?
UPDATE Swift 5.x
// duration will helps to control rotation speed
private func rotateView(targetView: UIView, duration: Double = 5) {
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: .pi)
}) { finished in
self.rotateView(targetView: targetView, duration: duration)
}
}
Swift 2.x way to rotate UIView indefinitely, compiled from earlier answers:
// Rotate <targetView> indefinitely
private func rotateView(targetView: UIView, duration: Double = 1.0) {
UIView.animateWithDuration(duration, delay: 0.0, options: .CurveLinear, animations: {
targetView.transform = CGAffineTransformRotate(targetView.transform, CGFloat(M_PI))
}) { finished in
self.rotateView(targetView, duration: duration)
}
}
UPDATE Swift 3.x
// Rotate <targetView> indefinitely
private func rotateView(targetView: UIView, duration: Double = 1.0) {
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(M_PI))
}) { finished in
self.rotateView(targetView: targetView, duration: duration)
}
}
Rotating a UIView through 360 degrees and more
Try this code:
CGFloat radians = atan2f(steeringWheel.transform.b, steeringWheel.transform.a);
if (radians < 0.0) radians += 2 * M_PI;
CGFloat degrees = radians * (180 / M_PI);
EDIT:
After rereading your question i saw where your problem really is. atan2 will always return results in range (−π, π].
And it looks you want the stearing wheel to be able to rotate a full circle to the left and a full cricle to the right. You can solve this by comparing new angle to the old angle so you'd know if user is rotating the wheel in CW or CCW.
You can also set a flag when user rotates the wheel left (CCW) from starting (idle) position to manage sign accordingly.
Clockwise image rotation animation
Try this
func rotateView(targetView: UIView, duration: Double = 1.0) {
UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat(M_PI))
}) { finished in
self.rotateView(targetView: YOUR_LOGO, duration: duration)
}
}
How to use
self.rotateView(targetView: YOUR_LOGO, duration: duration)
Rotate UIButton 360 degrees
You can use a trick: start rotating with 180 degrees first and then rotate with 360 degrees. Use 2 animations with delay.
Try this.
UIView.animate(withDuration: 0.5) {
self.view.transform = CGAffineTransform(rotationAngle: .pi)
}
UIView.animate(
withDuration: 0.5,
delay: 0.45,
options: UIView.AnimationOptions.curveEaseIn
) {
self.view.transform = CGAffineTransform(rotationAngle: 2 * .pi)
}
How can I rotate an image 360 degrees TWICE?
The "problem" is that when we get to the second animation, you are trying to set the view's transform
to CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI)
— but the view's transform
is already CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI)
, because that's what you set it to in the first animation! Thus, nothing happens because you are not asking to make any kind of change; you cannot animate a value to itself.
Related Topics
How to Present Uialertcontroller When Not in a View Controller
How to Find Topmost View Controller on Ios
What's the Difference Between the Atomic and Nonatomic Attributes
Set the Maximum Character Length of a Uitextfield
How to Pass Prepareforsegue: an Object
How to Return Value from Alamofire
How to Store Custom Objects in Nsuserdefaults
Can You Animate a Height Change on a Uitableviewcell When Selected
How to Get the Indexpath.Row When an Element Is Activated
Nsnotificationcenter Addobserver in Swift
How to Programmatically Send Sms on the Iphone
Undefined Symbols For Architecture Arm64
Split a String into an Array in Swift
Exc_Bad_Access Signal Received
Ios Uiimagepickercontroller Result Image Orientation After Upload
Check If My App Has a New Version on Appstore
Best Practices For Storyboard Login Screen, Handling Clearing of Data Upon Logout