0
let turnIndicator = UILabel(frame: CGRectMake(0, 0, 50, 50))
turnIndicator.alpha = 1
turnIndicator.backgroundColor = UIColor.clearColor()
turnIndicator.layer.backgroundColor = UIColor(red: 31/255.0, green: 174/255.0, blue: 240/255.0, alpha: 1.0).CGColor
turnIndicator.textColor = UIColor.blackColor()
turnIndicator.layer.cornerRadius = 25
turnIndicator.layer.borderColor = UIColor(red: 31/255.0, green: 174/255.0, blue: 240/255.0, alpha: 1.0).CGColor
turnIndicator.layer.borderWidth = 1
turnIndicator.textAlignment = NSTextAlignment.Center
turnIndicator.text = "↓"
currentView.view.addSubview(turnIndicator)

let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = M_PI
rotationAnimation.duration = 1.0

turnIndicator.layer.addAnimation(rotationAnimation, forKey: nil)

I tried this code to rotate my UILabel, but it didn't work, i had another code to do the rotation animation, it rotated successfully but didn't animate it.

Thanks!

Dror Chen
  • 51
  • 1
  • 10

2 Answers2

3

Yu can use UIView.animateWithDuration, like this:

UIView.animateWithDuration(3) { () -> Void in
      self.turnIndicator.layer.transform = CATransform3DMakeRotation(CGFloat(M_PI), 0, 0, 1);
}

UIView.animateWithDuration documentation here

CATransform3DMakeRotation documentation here

Dejan Skledar
  • 11,280
  • 7
  • 44
  • 70
  • it rotates it with no animation – Dror Chen Oct 30 '15 at 17:44
  • Thats not possible, I am just running this code myself with rotation. Are you using Xcode 7.1? What target? – Dejan Skledar Oct 30 '15 at 17:45
  • in the constructor of my class, my class inherits from NSObject – Dror Chen Oct 30 '15 at 17:52
  • When is the class initialized? Anywhere in the viewDidLoad or else ? – Dejan Skledar Oct 30 '15 at 17:52
  • in the ViewDidLoad of the view it appears in but not in the main viewcontroller, i just tried this code in my main viewcontroller and it worked, but here it doesn't, whats that all about? – Dror Chen Oct 30 '15 at 17:54
  • Its because you are calling your animation in the viewDidLoad function, at this time, the view is not yes visible, so no animation can be seen. To see the animation, you have to put your animation code in the viewDidAppear. – Dejan Skledar Oct 30 '15 at 18:04
  • another question, how do i choose the rotate direction? (clockwise or Counterclockwise) – Dror Chen Oct 30 '15 at 18:12
  • I think this question will help you out: http://stackoverflow.com/questions/27330252/objective-c-catransform3dmakerotation-clockwise-counterclockwise – Dejan Skledar Oct 30 '15 at 18:16
  • var perspectiveTransform = CATransform3DIdentity perspectiveTransform.m34 = 1.0 / -500 perspectiveTransform = CATransform3DRotate(perspectiveTransform, CGFloat(M_PI), 0, 0, 1) UIView.animateWithDuration(0.2, animations: { self.turnIndicator.layer.transform = perspectiveTransform }, completion: nil) tried this but it doesn't work – Dror Chen Oct 30 '15 at 18:27
0

Use AnimateWithDuration like this:

UIView.animateWithDuration(3/ 2, delay: 0, options: .Autoreverse, animations: {() -> Void in

var transform: CGAffineTransform = CGAffineTransformMakeRotation(M_PI)
    switchButton.transform = transform

}, completion: {(finished: Bool) -> Void in

    var transform: CGAffineTransform = CGAffineTransformIdentity
    switchButton.transform = transform
})
Paul Roub
  • 36,322
  • 27
  • 84
  • 93