试图在swift中动画化一个约束 [英] trying to animate a constraint in swift

查看:156
本文介绍了试图在swift中动画化一个约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITextField,我想放大它的宽度,当点击。我设置约束,并确保左侧的约束具有较低的优先级,那么我试图在右侧动画。



这里是代码

  //移动输入框
UIView.animateWithDuration(10.5,animations:{
self.nameInputConstraint.constant = 8
},完成:
中的
(值:Bool)println(>>> move const)
})

这可以工作,但似乎只是瞬间发生,似乎没有任何运动。我尝试设置它10秒,以确保我没有丢失任何东西,但我有相同的结果。



nameInputConstraint是我控制的约束的名称拖动

解决方案

div>

您需要先更改约束,然后对更新进行动画处理。

  self.nameInputConstraint.constant = 8 
UIView.animateWithDuration(0.5){
self.view。 layoutIfNeeded()
}

在Swift 3.0中:

  UIView.animate(withDuration:0.5){
self.view.layoutIfNeeded()
}


I have an UITextField that I want to enlarge it's width when tapped on. I set up the constraints and made sure the constraint on the left has the lower priority then the one that I am trying to animate on the right side.

Here is the code that I am trying to use.

  // move the input box
    UIView.animateWithDuration(10.5, animations: {
        self.nameInputConstraint.constant = 8
        }, completion: {
            (value: Bool) in
            println(">>> move const")
    })

This works, but it seems to just happen instantly and there doesn't seem to be any movement. I tried to set it 10 seconds to make sure I wasn't missing anything, but I got the same results.

nameInputConstraint is the name of the constraint that I control dragged to connect into my class from IB.

Thanks for your help in advance!

解决方案

You need to first change the constraint and then animate the update.

self.nameInputConstraint.constant = 8
UIView.animateWithDuration(0.5) {
    self.view.layoutIfNeeded()
}

And in Swift 3.0:

UIView.animate(withDuration: 0.5) {
    self.view.layoutIfNeeded()
}

这篇关于试图在swift中动画化一个约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆