【问题标题】:Want to flip two views so that one view hides and other shows swift想要翻转两个视图,以便一个视图隐藏而其他视图快速显示
【发布时间】:2021-10-22 06:57:36
【问题描述】:

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

我在目标 C 中找到了它(也许) 附上链接 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
        })
    }

【问题讨论】:

    标签: swift iphone xcode


    【解决方案1】:

    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:

    【讨论】:

    • @munib-hamza :) 很高兴我能帮上忙 :)
    猜你喜欢
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    相关资源
    最近更新 更多