想翻转两个视图,使一个视图隐藏另一个显示swift

Want to flip two views so that one view hides and other shows swift

我想从顶部或左侧翻转两个视图,方向不是什么大问题 基本上我想翻转一个视图,以便在动画中隐藏一个视图或和其他显示如卡片翻转 我想要这个但是有两个视图 https://www.youtube.com/watch?v=4kSLbuB-MlU

我在 objective C 中找到了它(也许) link附上 how to flip two views at once? 图片也已附上

我尝试了下面的方法,但找不到合适的解决方案

UIView.transition(from: vu1, to: vu2, duration: 1, options: .transitionFlipFromBottom, completion: { _ in
        })

也试过这个但没有帮助

   func flipTransition (with view1: UIView, view2: UIView, isReverse: Bool = true) {
        var transitionOptions = UIView.AnimationOptions()
        transitionOptions = isReverse ? [.transitionFlipFromLeft] : [.transitionFlipFromRight]

        UIView.transition(with: view1, duration: 1.5, options: transitionOptions, animations: {
            view1.isHidden = true
        })

        UIView.transition(with: view2, duration: 1.5, options: transitionOptions, animations: {
            view2.isHidden = false
        })
    }

UIView.transition(with 适用于容器视图,因此您可以尝试将子视图放入容器视图并在容器视图上应用 UIView.transition。因为你没有添加任何代码,所以我假设这里没有什么可以回答的

class ViewController: UIViewController {
    @IBOutlet weak var button: UIButton!
    let view1 = UIView()
    let view2 = UIView()
    let containerView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view1.backgroundColor = UIColor.red
        view2.backgroundColor = UIColor.green

        view.addSubview(containerView)
        containerView.addSubview(view1)
        containerView.addSubview(view2)

        containerView.translatesAutoresizingMaskIntoConstraints = false
        view1.translatesAutoresizingMaskIntoConstraints = false
        view2.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),

            view1.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            view1.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            view1.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
            view1.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 100),

            view2.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            view2.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            view2.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
            view2.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 100)
        ])

        view1.isHidden = false
        view2.isHidden = true
    }

    @IBAction func changeTapped() {
        UIView.transition(with: containerView,
                          duration: 1.0,
                          options: .transitionFlipFromBottom, animations: {[weak self] in
            guard let self = self else { return }
            self.view1.isHidden = !self.view1.isHidden
            self.view2.isHidden = !self.view2.isHidden
        })
    }

O/P: