【问题标题】:Animate UIButton with Autolayout with Swift使用 Swift 自动布局动画 UIButton
【发布时间】:2016-01-05 17:51:52
【问题描述】:

你好社区我只想为 UIView 设置一个 UIButton 的动画。但它真的不适合我。

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

这就是我的代码的样子。

【问题讨论】:

  • 省略self.view.needsUpdateConstraints(),它应该可以工作。你永远不能调用那个方法,如果你用 self.view.setNeedsUpdateConstraints() 替换它也应该没问题。

标签: ios swift animation uiview autolayout


【解决方案1】:

=> 试试这个:-

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

},

completion: { 

    UIView.animateWithDuration(0.6){

        self.button.transform = CGAffineTransformIdentity
    }
})

=> 如果你想用弹簧动画获得更多的弹跳效果。:-

=>尝试以下代码:

@IBOutlet 弱变量按钮: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()  }
 )
}

【讨论】:

    【解决方案2】:

    这真的很疯狂,但请确保您设置了正确的约束条件,这将对您有所帮助。

    它应该与这样的东西一起工作

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

    【讨论】:

      【解决方案3】:

      删除 self.view.needsUpdateConstraints()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 2015-11-11
        • 1970-01-01
        • 2019-06-05
        • 2023-03-26
        • 2014-07-13
        • 2015-09-01
        相关资源
        最近更新 更多