0

I'm trying to animate transform from '+' button to 'X' button by rotating it on click.

enter image description here

Everything works great when I'm using standard button on UIView. I'm using following code:

UIView.animateWithDuration(2.0, delay: 0.0, options: .CurveEaseOut, animations: {
        let transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
        self.navigationPlusButton.transform = transform
        }, completion: nil)

The problem occurs when I try to achieve the same with button in navigation bar.

enter image description here

Looks like button height is resized to 0 before the rotation and I don't know how to fix navigation button size since there are no constraints definitions. Note storyboard

enter image description here

and for constraints

enter image description here

Note: Second approach using CABasicAnimation rotated button nicely but it returned it to initial state immediately after animation ends. I would like to stay 'X' until I click again.

    let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
    rotateAnimation.fromValue = 0.0
    rotateAnimation.toValue = CGFloat(M_PI_4)
    rotateAnimation.duration = 2.0
    self.navigationPlusButton.layer.addAnimation(rotateAnimation, forKey: nil)

enter image description here

How it works so easily outside the navigation bar and it is so hard to do it on navigation bar? I can't find the solution. Please help

Swift Developer
  • 247
  • 2
  • 18

2 Answers2

1

Try this line on the CABasicAnimation:

rotateAnimation.removedOnCompletion = false

It may solve your problem.

Flavio Silverio
  • 1,004
  • 8
  • 12
1

try following lines to your code, it will solve your problem.

rotateAnimation.fillMode = kCAFillModeForwards
rotateAnimation.removedOnCompletion = false
Ruturaj Desai
  • 116
  • 1
  • 6