ios - 为什么我的动画不流畅?

标签 ios swift xcode9 uiviewanimation swift4.1

我正在尝试使用自定义类为文本字段设置动画,当我将左侧约束从 0 更改为 (self.bounds.width - self.bounds.width/3) 时,动画不平滑,但是当我设置时回到0就完美了。

这是我的自定义类的一部分:

lazy var leftConstraint = NSLayoutConstraint(item: placeholderLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0)


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.delegate = self
    self.addSubview(placeholderLabel)
    self.bringSubview(toFront: self)
    addConstraints()
}

func addConstraints() {

    placeholderLabel.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint(item: placeholderLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: self.bounds.height).isActive = true

    self.leftConstraint.isActive = true

    NSLayoutConstraint(item: placeholderLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0).isActive = true

    NSLayoutConstraint(item: placeholderLabel, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0).isActive = true


}


func textFieldDidBeginEditing(_ textField: UITextField) {
    animate(constant: self.bounds.width - self.bounds.width/3)
    self.bringSubview(toFront: placeholderLabel)

}


func textFieldDidEndEditing(_ textField: UITextField) {
    if (textField.text?.isEmpty)!{
        animate(constant: 0)
        self.bringSubview(toFront: self)
    }
}


func animate(constant: CGFloat) {

    self.leftConstraint.constant = constant

    UIView.animate(withDuration: 0.15) {
        self.layoutIfNeeded()
    }
}

最佳答案

动画不流畅的可能原因

func animate(constant: CGFloat) {
    /// Modifying Constraint out of animation
    /// This will animate in micro seconds and you wont get a 
    /// Smooth Animation
    self.leftConstraint.constant = constant
    /// Take this Line (Cut)

    UIView.animate(withDuration: 2) {
        /// Animation will take time to Happen
        self.leftConstraint.constant = constant
        /// Need to call this inside animation block
        self.view.layoutIfNeeded()
    }
}

尝试过的代码

import UIKit

class ViewController: UIViewController
{
    private var customView : UIView?
    private var leftConstraint : NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        customView = UIView()
        customView?.backgroundColor = .red
        leftConstraint = NSLayoutConstraint()
        addConstraints()

        DispatchQueue.main.async {
            self.animateView()
        }
    }

    func addConstraints(){
        self.view.addSubview(customView!)
        customView?.translatesAutoresizingMaskIntoConstraints = false
        leftConstraint = customView?.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 50)
        leftConstraint?.isActive = true
        customView?.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50).isActive = true
        customView?.heightAnchor.constraint(equalToConstant: 50).isActive = true
        customView?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
    }

    func animateView(){
        UIView.animate(withDuration: 2) {
            /// Animation will take time to Happen
            self.leftConstraint?.constant = 100
            /// Need to call this inside animation block
            self.view.layoutIfNeeded()
        }
    }
}

输出

enter image description here

关于ios - 为什么我的动画不流畅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459138/

相关文章:

ios - 后退按钮不会自动本地化

ios - 在指定时间间隔使用 CoreLocation 在后台检索 CoreMotion 数据

iOS 10 Swift 和定义 UI 的最佳实践

ios - 在 Xcode 中为 native iOS 开发的应用程序

swift4 - Vapor 不适用于 xcode 9 和 swift 4

ios - 从 subview 返回主视图

ios - 在两个 Action 之间随机选择并每隔一定时间重复一次

ios - 如何在 AsyncDisplayKit 中使用 scrollView 函数

swift - 如何正确管理 `AnyCancellable` 的集合

xcode9 - xCode 9.0 beta (9M136h) 在设备 : Failed to find a suitable device for the type IBSimDeviceTypeiPad2x 上运行