swift - 使用 Snapkit 查看动画

标签 swift snapkit

我尝试了不同的教程来使用 Snapkit 制作灯光动画,但似乎没有任何帮助。我什至尝试过 Spring 框架,但效果不佳。这是我的代码。

private func setupMatchView() {
    view.addSubview(matchOverlayView)
    matchOverlayView.snp.makeConstraints { (make) in
        make.top.equalTo(view.safeAreaLayoutGuide.snp.topMargin)
        make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottomMargin)
        make.left.equalToSuperview()
        make.right.equalToSuperview()
    }
    matchOverlayView.backgroundColor = UIColor.white.withAlphaComponent(0.5)

    view.addSubview(matchView)
    matchView.snp.makeConstraints { (make) in
        make.left.equalToSuperview().offset(32)
        make.right.equalToSuperview().inset(32)
        make.centerX.equalToSuperview()
        make.centerY.equalToSuperview()
    }
    matchView.backgroundColor = .white
    matchView.layer.cornerRadius = 8
    matchView.dropShadow()

    matchView.addSubview(matchYourImageView)
    matchYourImageView.snp.makeConstraints { (make) in
        make.height.width.equalTo(100)
        make.centerX.equalToSuperview().inset(20)
        make.top.equalToSuperview().offset(40)
    }
    matchYourImageView.backgroundColor = UIColor(red:0.77, green:0.77, blue:0.77, alpha:1.00)
    matchYourImageView.layer.cornerRadius = 50
    matchYourImageView.clipsToBounds = true

    matchView.addSubview(matchHerImageView)
    matchHerImageView.snp.makeConstraints { (make) in
        make.height.width.equalTo(100)
        make.centerX.equalToSuperview().inset(-20)
        make.top.equalTo(matchYourImageView.snp.top)
    }
    matchHerImageView.backgroundColor = UIColor(red:0.69, green:0.69, blue:0.69, alpha:1.00)
    matchHerImageView.layer.cornerRadius = 50
    matchHerImageView.clipsToBounds = true

    matchView.addSubview(matchLabel)
    matchLabel.snp.makeConstraints { (make) in
        make.top.equalTo(matchYourImageView.snp.bottom).offset(64)
        make.centerX.equalToSuperview()
    }
    matchLabel.text = "Match"
    matchLabel.font = UIFont.systemFont(ofSize: 28, weight: .heavy)

    matchView.addSubview(matchCTA)
    matchCTA.snp.makeConstraints { (make) in
        make.top.equalTo(matchLabel.snp.bottom).offset(40)
        make.left.equalToSuperview().offset(32)
        make.right.equalToSuperview().inset(32)
        make.height.equalTo(44)
        make.bottom.equalToSuperview().inset(40)
    }
    matchCTA.setTitle("Go To Chat", for: .normal)


    matchView.addSubview(matchCloseButton)
    matchCloseButton.snp.makeConstraints { (make) in
        make.width.equalTo(32)
        make.height.equalTo(32)
        make.right.equalToSuperview().inset(16)
        make.top.equalToSuperview().inset(16)
    }
    matchCloseButton.setImage(UIImage(named: "Close"), for: .normal)
    matchCloseButton.addTarget(self, action: #selector(self.closeButtonTapped(_:)), for:.touchUpInside)

    matchView.layoutIfNeeded()

    hideMatch()
}

@objc func closeButtonTapped(_ sender:UIButton) {
    hideMatch()
}

private func hideMatch() {
    self.matchOverlayView.alpha = 0
    self.matchView.snp.updateConstraints { (make) in
        make.centerY.equalToSuperview().offset(self.view.frame.height)
    }
    UIView.animate(withDuration: 1) {
        self.matchView.layoutIfNeeded()
    }
}
private func showMatch() {
    self.matchOverlayView.alpha = 1
    self.matchView.snp.updateConstraints { (make) in
        make.centerY.equalToSuperview().offset(0)
    }
    UIView.animate(withDuration: 2) {
        self.matchView.layoutIfNeeded()
    }
}

那是行不通的。当用户触发 showMatch() 方法时,该 View 只是显示,没有动画。如果有人可以提供帮助,那就太好了。

最佳答案

您必须在 matchView 的 super View 上调用 layoutIfNeeded:

private func showMatch() {
    self.matchOverlayView.alpha = 1
    self.matchView.snp.updateConstraints { (make) in
        make.centerY.equalToSuperview().offset(0)
    }
    UIView.animate(withDuration: 2) {
        self.view.layoutIfNeeded()
    }
}

当您在 UIView 上调用 layoutIfNeeded 时,布局引擎会将该 View 用作 Root View 并开始布局该 View 的所有 subview 。但只有 subview !

这就是为什么当您在 matchView 上调用 layoutIfNeeded 时布局更改不是动画的原因。您必须在 matchView 的 super View 之一上调用 layoutIfNeeded 以触发布局更改。

关于swift - 使用 Snapkit 查看动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304138/

相关文章:

ios - UICollectionView 自定义单元格访问单元格外部的属性

C# 绑定(bind)库方法不接受实现参数

ios - 如何使用自动调整高度的 SnapKit 以编程方式创建 UITableViewCell?

swift - 如何将图像保存在用户选择的文件夹中,而不是我选择的文件夹中

ios - 有没有更好的方法在 SwiftUI 中创建多列(数据表) ListView

ios - 弹性布局不适用于 subview Controller

ios - 调用layoutIfNeeded后 View 未立即显示

ios - 为什么标签的高度为零?

ios - 使用 NSURL 无法识别 URL 变量

swift - 用户已经登录并使用api将数据保存到核心数据中