iOS / Swift-使用SteviaLayout的视图/键盘动画会在第二次点击后发生

时间:2018-07-17 17:28:02

标签: ios swift autolayout

我已经为我的较大项目构建了模板UI。尝试使用SteviaLayout实现键盘动画,但无法对其进行管理。

之后,我尝试使用UITapGestureRecognizer模拟该动画,但是UI在第二次调用之前没有响应。

毕竟,主要问题是;

  

我想用键盘为“ bottomView”设置动画。

仅将ViewController上载到Github Gist。您可以复制并粘贴代码。

对于不想走Gist的人,也粘贴在这里:

import UIKit
import Stevia
import Hero

class ViewController: UIViewController, UITextFieldDelegate {

    lazy var topView: UIView = {
        let v = UIView()
        v.backgroundColor = .red
        return v
    }()

    lazy var bottomView: UIView = {
        let v = UIView()
        v.backgroundColor = .blue
        return v
    }()

    lazy var collectionView: UIView = {
        let v = UIView()
        v.backgroundColor = .green
        return v
    }()

    lazy var addButtonView: UIView = {
        let v = UIView()
        v.backgroundColor = .yellow
        v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleKeyboard)))
        v.isUserInteractionEnabled = true
        return v
    }()

    @objc func handleKeyboard() {

        self.view.layout(
            |-0-self.bottomView.height(50)-0-|,
            150,
            |-0-self.collectionView.height(50)-0-|,
            0
        )

        self.view.setNeedsLayout()

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

    lazy var sendButtonView: UIView = {
        let v = UIView()
        v.backgroundColor = .yellow
        v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleKeyboardDismiss)))
        v.isUserInteractionEnabled = true
        return v
    }()

    @objc func handleKeyboardDismiss() {

        view.setNeedsLayout()

        self.view.layout(
            |-0-self.bottomView.height(50)-0-|,
            0,
            |-0-self.collectionView.height(50)-0-|,
            0
        )

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

    lazy var textField: UITextField = {
        let tf = UITextField()
        tf.borderStyle = .roundedRect
        tf.layer.cornerRadius = 20
        tf.layer.masksToBounds = true
        tf.returnKeyType = .done
        return tf
    }()

    lazy var backgroundView: UIView = {
        let v = UIView()
        v.backgroundColor = .brown
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        textField.delegate = self

        view.sv(
            backgroundView,
            topView,
            bottomView.sv(
                addButtonView,
                textField,
                sendButtonView
            ),
            collectionView
        )

        view.layout(
            0,
            |-0-backgroundView-0-|,
            0
        )

        view.layout(
            0,
            |-0-topView.height(100)-0-|
        )

        view.layout(
            |-0-bottomView.height(50)-0-|,
            0,
            |-0-collectionView.height(50)-0-|,
            0
        )

        bottomView.layout(
            5,
            |-5-addButtonView.width(40)-5-textField-5-sendButtonView.width(40).heightEqualsWidth()-5-|,
            5
        )

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShowAnimation), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHideAnimation), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    @objc func keyboardWillShowAnimation(notification: Notification) {
        guard let keyboardRect = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else  { return }

        if notification.name == NSNotification.Name.UIKeyboardWillShow {
            self.view.setNeedsLayout()

            self.view.layout(
                |-0-self.bottomView.height(50)-0-|,
                keyboardRect.height,
                |-0-self.collectionView.height(50)-0-|,
                0
            )

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

    @objc func keyboardWillHideAnimation(notification: Notification) {
        //guard let keyboardRect = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else  { return }
        if notification.name == NSNotification.Name.UIKeyboardWillHide {

            UIView.animate(withDuration: 0.5) {

                self.view.layout(
                    |-0-self.bottomView.height(50)-0-|,
                    0,
                    |-0-self.collectionView.height(50)-0-|,
                    0
                )

                self.view.layoutIfNeeded()
            }
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        view.endEditing(true)
        return true
    }
}

0 个答案:

没有答案