简体   繁体   中英

Animate UIButton with Autolayout with Swift

Hello Community I just want to animate an UIView exactly an UIButton. But it really don't work for me.

 self.introConstraints.constant = self.view.frame.width
    self.view.needsUpdateConstraints()
    UIView.animateWithDuration(0.6){
        self.view.layoutIfNeeded()
    }

This is how my code looks like.

=> try this one :-

UIView.animateWithDuration(0.6 , animations: { self.button.transform = CGAffineTransformMakeScale(0.6, 0.6)

},

completion: { 

    UIView.animateWithDuration(0.6){

        self.button.transform = CGAffineTransformIdentity
    }
})

=> If you want to more bouncing effect with spring animation.:-

=>try folowing code:

@IBOutlet weak var button: UIButton!

@IBAction func animateButton(sender: AnyObject) {

    button.transform = CGAffineTransformMakeScale(0.6, 0.6)

   UIView.animateWithDuration(2.0,

   delay: 0,
   usingSpringWithDamping: CGFloat(0.20),

   initialSpringVelocity: CGFloat(6.0),

   options: UIViewAnimationOptions.AllowUserInteraction,

   animations: {

        self.button.transform = CGAffineTransformIdentity
   },
      completion: { Void in()  }
 )
}

It's really crazy but guys make sure you set the correct constraints that will help you.

it should work with something like this

 self.startConstraints.constant = self.view.frame.width - self.startButton.frame.width
    UIView.animateWithDuration(0.6){
        self.view.layoutIfNeeded()
    }

删除self.view.needsUpdateConstraints()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM