2015-12-05 5 views
8

Ich habe versucht, eine Animation zu laufen, dass meine UIButton 360 Grad dreht, aber wenn ich dies tun:Drehen UIButton 360 Grad - Swift

UIView.animateWithDuration(3.0, animations: { 
    self.vineTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2)) 
    self.instagramTimeCapButton.transform = CGAffineTransformMakeRotation(CGFloat(M_PI*2)) 
    }) 

Es dreht sich nicht um 360 Grad, da die UIButton ist bereits bei dieser Ort.

Wie kann ich meinen UIButton 360 Grad drehen?

Antwort

22

Sie können einen Trick verwenden: Starten Sie zuerst mit 180 Grad drehen und dann mit 360 Grad drehen. Verwenden Sie 2 Animationen mit Verzögerung. Versuchen Sie dies.

UIView.animateWithDuration(0.5) {() -> Void in 
    button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 
} 

UIView.animateWithDuration(0.5, delay: 0.45, options: UIViewAnimationOptions.CurveEaseIn, animations: {() -> Void in  
    button.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 2)) 
}, completion: nil) 

Swift 4

UIView.animate(withDuration: 0.5) {() -> Void in 
    self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi) 
} 

UIView.animate(withDuration: 0.5, delay: 0.45, options: UIViewAnimationOptions.curveEaseIn, animations: {() -> Void in 
    self.settingsButton.transform = CGAffineTransform(rotationAngle: CGFloat.pi * 2.0) 
}, completion: nil) 

this helps

+2

SIE SIND ERSTAUNLICH !! Einziger Vorschlag: Um eine glatte Drehung von 360 zu erreichen, muss die Verzögerung für die zweite "animateWithDuration" halb so groß sein wie die Animationsdauer. Vielen Dank !! –

6

in Swift 3:

UIView.animate(withDuration:0.5, animations: {() -> Void in 
    button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI)) 
}) 

UIView.animate(withDuration: 0.5, delay: 0.45, options: .curveEaseIn, animations: {() -> Void in 
    button.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI * 2)) 
}, completion: nil) 
1

Swift 4: Animation verschachtelter Verschluss ist besser als Animation Verzögerungsblock .

UIView.animate(withDuration: 0.5, animations: { 
    button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) 
}) { (isAnimationComplete) in 

     // Nested Block 
UIView.animate(withDuration: 0.5) { 
    button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi * 2))) 
     }  
} 
2

Wie diskutiert here, können Sie auch eine CAAnimation verwenden können. Dieser Code gilt eine volle Umdrehung 360:

Swift 3

let fullRotation = CABasicAnimation(keyPath: "transform.rotation") 
fullRotation.delegate = self 
fullRotation.fromValue = NSNumber(floatLiteral: 0) 
fullRotation.toValue = NSNumber(floatLiteral: Double(CGFloat.pi * 2)) 
fullRotation.duration = 0.5 
fullRotation.repeatCount = 1 
button.layer.add(fullRotation, forKey: "360") 

Sie Quartz importieren müssen:

import QuartzCore 

Und Ihre Viewcontroller muss CAAnimationDelegate entsprechen:

class ViewController: UIViewController, CAAnimationDelegate { 

} 
+0

Dies ist die richtige Antwort! –

0

Sie könnenverwendenErweiterung (Swift 3.x):

extension UIView { 
    func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) { 
     let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
     rotateAnimation.fromValue = 0.0 
     rotateAnimation.toValue = CGFloat(.pi * 2.0) 
     rotateAnimation.duration = duration 

     if let delegate: AnyObject = completionDelegate { 
      rotateAnimation.delegate = delegate as? CAAnimationDelegate 
     } 
     self.layer.add(rotateAnimation, forKey: nil) 
    } 
} 

Nutzungs:

self.vineTimeCapButton.rotate360Degrees() 

oder:

self. vineTimeCapButton.rotate360Degrees(duration:2) 
0

Swift 4 Version, die Sie gleich drehen erlaubt Ansicht unendlich Zeiten:

UIView.animate(withDuration: 0.5) { 
    self.yourButton.transform = self.yourButton.transform.rotated(by: CGFloat.pi) 
}