Xcode 尝试在swift中设置约束动画

Xcode 尝试在swift中设置约束动画,xcode,swift,constraints,xcode6,Xcode,Swift,Constraints,Xcode6,我有一个UITextField,我想在点击时放大它的宽度。我设置了约束,并确保左侧的约束优先级低于我试图在右侧设置动画的约束优先级 下面是我正在尝试使用的代码 //移动输入框 UIView.animateWithDuration(10.5,动画:{ self.nameInputConstraint.constant=8 },完成日期:{ (值:Bool)in println(“>>>移动常量”) }) 这是可行的,但似乎只是瞬间发生,似乎没有任何运动。我试着设置10秒,以确保我没有遗漏任何东西

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

下面是我正在尝试使用的代码

//移动输入框
UIView.animateWithDuration(10.5,动画:{
self.nameInputConstraint.constant=8
},完成日期:{
(值:Bool)in
println(“>>>移动常量”)
})
这是可行的,但似乎只是瞬间发生,似乎没有任何运动。我试着设置10秒,以确保我没有遗漏任何东西,但我得到了相同的结果


nameInputConstraint
是我控制从IB拖动到类中连接的约束的名称。

您需要首先更改约束,然后设置更新动画。
这应该在superview中

self.nameInputConstraint.constant = 8
Swift 2

UIView.animateWithDuration(0.5) {
    self.view.layoutIfNeeded()
}
Swift 3、4、5

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

在我的例子中,我只更新了自定义视图

// DO NOT LIKE THIS
customView.layoutIfNeeded()    // Change to view.layoutIfNeeded()
UIView.animate(withDuration: 0.5) {
   customViewConstraint.constant = 100.0
   customView.layoutIfNeeded() // Change to view.layoutIfNeeded()
}

使用Swift 5和iOS 12.3,根据您的需要,您可以选择以下三种方法之一来解决您的问题


#1.使用
UIView
animate(持续时间:animations:)
class方法 有以下声明:

使用指定的持续时间为一个或多个视图的更改设置动画

下面的游乐场代码显示了
动画(withDuration:animations:)
的可能实现,以便为自动布局约束的恒定更改设置动画

导入UIKit
导入PlaygroundSupport
类ViewController:UIViewController{
让textView=UITextView()
lazy var heightConstraint=textView.heightAnchor.constraint(equalToConstant:50)
重写func viewDidLoad(){
view.backgroundColor=.white
view.addSubview(文本视图)
textView.backgroundColor=.orange
textView.isEditable=false
textView.text="知识本身是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德如果你不自以为是,就必须为自己的行为负责。”
textView.translatesAutoResizezingMaskintoConstraints=false
textView.topAnchor.constraint(EqualToSystemsPacingDown:view.layoutMarginsGuide.topAnchor,乘数:1)。isActive=true
textView.leadingAnchor.constraint(equalTo:view.layoutMarginsGuide.leadingAnchor).isActive=true
textView.trailingAnchor.constraint(equalTo:view.layoutMarginsGuide.trailingAnchor).isActive=true
heightConstraint.isActive=true
让Tap手势=UITapGestureRecognitor(目标:自我,操作:#选择器(doIt(:))
textView.AddGestureRecognitor(点击手势)
}
@objc func doIt(uu发送方:UITAPGESTURE识别器){
heightConstraint.constant=heightConstraint.constant==50?150:50
UIView.animate(持续时间:2){
self.view.layoutifneed()
}
}
}
PlaygroundPage.current.liveView=ViewController()

#2.使用
UIViewPropertyImator
init(持续时间:曲线:动画:)
初始化器和
startAnimation()
方法 有以下声明:

使用内置的UIKit计时曲线初始化动画师

下面的游乐场代码显示了
init(持续时间:曲线:动画:)
startAnimation()
的可能实现,以便为自动布局约束的不断更改设置动画

导入UIKit
导入PlaygroundSupport
类ViewController:UIViewController{
让textView=UITextView()
lazy var heightConstraint=textView.heightAnchor.constraint(equalToConstant:50)
重写func viewDidLoad(){
view.backgroundColor=.white
view.addSubview(文本视图)
textView.backgroundColor=.orange
textView.isEditable=false
textView.text=”知识本身是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德,是一种美德如果你不自以为是,就必须为自己的行为负责。”
textView.translatesAutoResizezingMaskintoConstraints=false
textView.topAnchor.constraint(EqualToSystemsPacingDown:view.layoutMarginsGuide.topAnchor,乘数:1)。isActive=true
textView.leadingAnchor.constraint(equalTo:view.layoutMarginsGuide.leadingAnchor).isActive=true
textView.trailingAnchor.constraint(equalTo:view.layoutMarginsGuide.trailingAnchor).isActive=true
heightConstraint.isActive=true
让Tap手势=UITapGestureRecognitor(目标:自我,操作:#选择器(doIt(:))
textView.AddGestureRecognitor(点击手势)
}
@objc func doIt(uu发送方:UITAPGESTURE识别器){
heightConstraint.constant=heightConstraint.constant==50?150:50
让animator=UIViewPropertyAnimator(持续时间:2,曲线:。线性,动画:{
self.view.layoutifneed()
})
动画师
}
}
PlaygroundPage.current.liveView=ViewController()

#3.使用
UIViewPropertyAnimator
运行PropertyAnimator(持续时间:延迟:选项:动画:完成:)
类方法 有以下声明:

创建并返回立即开始运行其动画的animator对象

操场代码如下
class func animate(withDuration duration: TimeInterval, animations: @escaping () -> Void)
convenience init(duration: TimeInterval, curve: UIViewAnimationCurve, animations: (() -> Void)? = nil)
class func runningPropertyAnimator(withDuration duration: TimeInterval, delay: TimeInterval, options: UIViewAnimationOptions = [], animations: @escaping () -> Void, completion: ((UIViewAnimatingPosition) -> Void)? = nil) -> Self
self.mConstraint.constant = 100.0
UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
}
self.mConstraint.constant = 100
UIView.animate(withDuration: 0.3, animations: {
        self.view.layoutIfNeeded()
    }, completion: {res in
        //Do something
})
    topConstraint.constant = heightShift

    UIView.animate(withDuration: 0.3) {

        // request layout on the *superview*
        self.view.superview?.layoutIfNeeded()
    }
class MyClass {

    /// Container view
    let container = UIView()
        /// View attached to container
        let view = UIView()

    /// Top constraint to animate
    var topConstraint = NSLayoutConstraint()


    /// Create the UI hierarchy and constraints
    func createUI() {
        container.addSubview(view)

        // Create the top constraint
        topConstraint = view.topAnchor.constraint(equalTo: container.topAnchor, constant: 0)


        view.translatesAutoresizingMaskIntoConstraints = false

        // Activate constaint(s)
        NSLayoutConstraint.activate([
           topConstraint,
        ])
    }

    /// Update view constraint with animation
    func updateConstraint(heightShift: CGFloat) {
        topConstraint.constant = heightShift

        UIView.animate(withDuration: 0.3) {

            // request layout on the *superview*
            self.view.superview?.layoutIfNeeded()
        }
    }
}
UIView.animate(withDuration: 1.0, animations: {
       self.centerX.constant -= 75
       self.view.layoutIfNeeded()
}, completion: nil)