ios - 使用 SnapKit 时无法正确设置动画

标签 ios snapkit

我正在尝试为我的应用程序制作 Logo (UILabel) 从中间到顶部的动画。我尝试的是更新约束,但它似乎不起作用。问题是动画,即 Logo ,从原点 (0,0) 而不是 View 的中间到顶部。必要的代码( Controller 及其继承的类):

import UIKit
import SnapKit

class EntryController: LatroController {

    static let spacingFromTheTop: CGFloat = 150
    var latroLabelCenterYConstraint: Constraint?

    override init() {
        super.init()
        self.animateTitleLabel()
    }

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

    override func initTitleLabel() {
        self.latroLabel = UILabel()
        self.latroLabel?.text = General.latro.rawValue
        self.latroLabel?.textAlignment = .center
        self.latroLabel?.font = UIFont (name: General.latroFont.rawValue, size: EntryController.fontSize)
        self.latroLabel?.textColor = .white
        self.latroLabel?.contentMode = .center
        self.view.addSubview(self.latroLabel!)

        self.latroLabel?.snp.makeConstraints({ (make) in
            make.width.equalTo(EntryController.latroWidth)
            make.height.equalTo(EntryController.latroHeight)
            make.centerX.equalTo(self.view.center.x)
            self.latroLabelCenterYConstraint = make.centerY.equalTo(self.view.center.y).constraint
        })
    }

    func animateTitleLabel() {
        UIView.animate(withDuration: 1.5) {
            self.latroLabel?.snp.updateConstraints { (make) in
                make.centerY.equalTo(200)
            }
            self.view.layoutIfNeeded()
        }
    }
}

import UIKit
import SnapKit

class LatroController: UIViewController {

static let latroWidth: CGFloat = 288
static let latroHeight: CGFloat = 98
static let btnWidth: CGFloat = 288
static let btnHeight: CGFloat = 70
static let txtFieldWidth: CGFloat = 288
static let txtFieldHeight: CGFloat = 50
static let fontSize: CGFloat = 70
static let bottomOffset: CGFloat = 100
static let buttonOffset: CGFloat = 20
static let logoOffset: CGFloat = 50

var latroLabel: UILabel?
var signUpBtn: UIButton?
var logInBtn: UIButton?
var titleLabelYConstraint: NSLayoutConstraint?
var usernameTxtField: UITextField?

init() {
    super.init(nibName: nil, bundle: nil)
    self.view.backgroundColor = UIColor(named: General.orange.rawValue)
    self.initTitleLabel()
}

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

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: false)
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

func initTitleLabel() {
    self.latroLabel = UILabel()
    self.latroLabel?.text = General.latro.rawValue
    self.latroLabel?.textAlignment = .center
    self.latroLabel?.font = UIFont (name: General.latroFont.rawValue, size: EntryController.fontSize)
    self.latroLabel?.textColor = .white
    self.latroLabel?.contentMode = .center
    self.view.addSubview(self.latroLabel!)

    self.latroLabel?.snp.makeConstraints({ (make) in
        make.width.equalTo(LatroController.latroWidth)
        make.height.equalTo(LatroController.latroHeight)
        let safeAreaLayoutHeight = self.view.safeAreaLayoutGuide.layoutFrame.height
        print(safeAreaLayoutHeight)
        make.top.equalTo(self.view).offset(150)
        make.centerX.equalTo(self.view.center.x)
    })
}
}

最佳答案

在界面中并执行初始布局之前,您无法为 View 设置动画。因此,您调用 self.animateTitleLabel() 的方式太早了(在 init 中)。

viewDidAppear 中调用它。当然,您必须使用 Bool 标志属性来确保您不会在viewDidAppear 运行时每次 调用它,只有第一次 调用它。

(可能需要在 viewDidLayoutSubviews 中调用它;您必须进行试验。)

关于ios - 使用 SnapKit 时无法正确设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007330/

相关文章:

ios - 更新约束不适用于 UITableView snapkit

ios - Swift 3 - 比较两个日期时对 < 运算符的使用不明确

ios - UILabel 未在原型(prototype)单元中呈现

ios - 在 DeckView 示例中将右 View 移到中心 View 上

ios - 由于未捕获的异常无法安装约束而终止应用程序,原因 : No common superview between views

ios - swift:swift 中的 SnapKit 不起作用

ios - 快速转换在类签名中具有泛型的具体类型

ios - React Native iOS pod 设置有重复的目标问题

ios - 自定义TableViewCell触发NSLayoutConstraint错误 'UIView-Encapsulated-Layout-Height'

ios - 如何使用 View 数组中具有最高高度的 View 进行约束