【问题标题】:How To Transition Between Subviews While Being In The Same viewController without Navigation Controller? [closed]如何在没有导航控制器的同一个视图控制器中时在子视图之间转换? [关闭]
【发布时间】:2020-08-24 18:44:18
【问题描述】:

我是 iOS 开发的新手。我想在 UIKit 的帮助下编写一个页面,我可以在子视图之间切换(通过单击单选按钮)并带有不错的动画(如本视频所示: https://www.youtube.com/watch?v=ZXXWkieFzN8)。但是,我不知道如何做这样的事情。我正在尝试在我的 reset passowrd 页面中实现此逻辑,用户可以选择最佳选项来重置密码。

【问题讨论】:

    标签: ios swift navigation ios-animations


    【解决方案1】:

    从标准 SDK 有 UIPageViewController https://developer.apple.com/documentation/uikit/uipageviewcontroller

    但如果您需要自定义动画/视图,则更容易:

    1. 创建持有人 UIView,您将在其中更改内容

    2. 为您的自定义内容创建不同的 UIView(或 UIViewController)

    3. 当您需要更改内容时 - 您从持有者中移除之前的 UIView 并插入新的 UIView(从而交换它们)。

    我尝试使用 UIViewControllers 而不是 UIView 的内容,因为它更可重用。

    给你一个例子

    class DynamicViewManager {
        
        private weak var contentView: UIView?
        private(set) weak var currentShownVC: UIViewController?
        
        
        init(contentView: UIView) {
       
            self.contentView = contentView
        }
        
        
        func show(viewController: UIViewController) {
            
            guard let contentView = self.contentView else {
                
                return
            }
            
            viewController.view.translatesAutoresizingMaskIntoConstraints = false
            
            self.removePresentedVC()
            self.currentShownVC = viewController
            
            contentView.addSubview(viewController.view)
            contentView.sendSubviewToBack(viewController.view)
            NSLayoutConstraint.activate(self.pinningConstraints(view: viewController.view, to: contentView))
        }
        
        
        @discardableResult
        func removePresentedVC() -> UIViewController? {
            
            let cached = self.currentShownVC
            self.currentShownVC?.view.removeFromSuperview()
            self.currentShownVC = nil
            return cached
        }
    
        private func pinningConstraints(view: UIView, to anotherView: UIView) -> [NSLayoutConstraint] {
        
          return [
                view.leadingAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.leadingAnchor),
                view.trailingAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.trailingAnchor),
                view.topAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.topAnchor),
                view.bottomAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.bottomAnchor)
            ]
        }
    }
    

    在你的情况下,它看起来像这样

    let manager = DynamicViewManager(contentView: self.contentView)
    let left = LeftViewController()
    let right = RightViewController()
    .onLeftClick {
        manager.show(left) 
        self.changeHeight(for: contentView, toPercentage: 15)
    }
    
    .onRightClick {
        manager.show(right)
        self.changeHeight(for: contentView, toPercentage: 30)
    }
    

    你还可以调整ContentView的高度,这样你下面的所有内容(重置密码也移到顶部)

    您可以通过停用先前的约束并激活新的约束来更改高度(以使 self.changeHeight 起作用)

    【讨论】: