0

In my app I have two sort buttons that looks like a custom segmented control.

The VC inits with Button1 selected. If you then press on Button2 a UIView will animate from Button1 to Button2 - this is effect to show what button is selected

My code looks like this:

@IBAction func BtnOneDidTouch(sender: AnyObject) {
        myLabel.text = "Button 1"
        UIView.animateWithDuration(0.7,
            delay: 0,
            usingSpringWithDamping: 0.7,
            initialSpringVelocity: 0.5,
            options: nil,
            animations: {
                self.underLine.center = CGPointMake(self.btnOne.center.x, self.btnOne.center.y + 16)
            }, completion: nil
        )
    }


    @IBAction func BtnTwoDidTouch(sender: AnyObject) {
        myLabel.text = "Button 2"
        UIView.animateWithDuration(0.7,
            delay: 0,
            usingSpringWithDamping: 0.7,
            initialSpringVelocity: 0.5,
            options: nil,
            animations: {
                self.underLine.center = CGPointMake(self.btnTwo.center.x, self.btnTwo.center.y + 16)
            }, completion: nil
        )
    }

underLine is the view that is being animated.

If I comment out

myLabel.text = "Button 1"

and myLabel.text = "Button 2"

from the functions it will work. But If I leave them, it does not work.

It fails by: If you click on Btn2 the view will slide then jump back again and only the text label will be changed.

What I want to is to have the view slide and stay and change the label as well.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2722667
  • 8,195
  • 14
  • 52
  • 100
  • As I understand you need to change label's text with animation? – Nikita Zernov Sep 08 '15 at 21:03
  • @NikitaZernov Yeah. I want to show different content depending on what button is selected – user2722667 Sep 08 '15 at 21:05
  • The problem is likely down to constraints. The view you are animating probably has constraints you gave it or which have been given to it by xcode. When you set the text, it is causing layout to fire which is warping the view back to the auto layout location. Try setting the animated views `translatesAutoresizingMaskIntoConstraints` property to `NO`. Alternatively give the view a leading and top constraint, CTRL drag these to create IBOutlets, then in code change the constraint constants and animate the constraint change. – Rory McKinnel Sep 08 '15 at 21:10
  • @RoryMcKinnel Thanks, I made some constriants and then changed them and called layoutIfNeeded. Now it works – user2722667 Sep 08 '15 at 22:17
  • Good news. You probably already know and did this, but for constraint animation you change the constraint before the animation block and put layoutIfNeeded as the animation block code to get animation to work properly. – Rory McKinnel Sep 08 '15 at 22:26
  • @RoryMcKinnel I had no idea. Should I first call self.myConst.constant = 14 then call animateWithDur and have layoutIfNeeded inside that? It still works/looks perfect with having everything inside the animateDur function – user2722667 Sep 08 '15 at 23:19
  • Yes, you should call layoutIfNeeded, then set the constants before the animation then all you need in the animation block is the layoutIfNeeded line. See here: http://stackoverflow.com/questions/12622424/how-do-i-animate-constraint-changes – Rory McKinnel Sep 08 '15 at 23:38
  • @RoryMcKinnel thanks for telling me! I been doing it wrong all over my code.. – user2722667 Sep 08 '15 at 23:43

1 Answers1

1

The problem is likely down to constraints. The view you are animating probably has constraints you gave it or which have been given to it by xcode.

When you set the text, it is causing layout to fire which is warping the view back to the initial auto layout location.

Try setting the animated views translatesAutoresizingMaskIntoConstraints property to NO.

Alternatively, give the view a leading and top constraint, CTRL drag these to create IBOutlets, then in code change the constraint constants and animate the constraint change.

For details on how to animate constraints see: How do I animate constraint changes?

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28