繁体   English   中英

从子VC设置父VC对象的动画

[英]Animate parent VC object from a child VC

在Swift中可以为父VC的视图设置动画吗?

我有一个带有UIView的root / master VC,我将其用作UITabBarController,所以我的4个主要VC的其余部分都是root的子级。

在某些子级VC上,我的子视图应该占据整个屏幕,而看不到根VC的自定义选项卡栏(UIView),但它仍漂浮在上方。

每当我打开全屏子视图时,我都希望它通过Y轴从屏幕上滑出,但是由于它在运行时返回nil ,所以我似乎无法访问或操纵根VCs属性。

这是自定义标签栏根VC,因此您可以了解代码的结构:

class RootVC: UIViewController {

    //This is where we pull all of our content from other VCs
    //when a tab bar button is selected
    @IBOutlet weak var contentView: UIView!

    //The custom tab bar itself with an array of button outlets
    @IBOutlet public weak var customTabBarContainer: UIView!
    @IBOutlet var tabBarButtons: [UIButton]!

    //4 main view VCs that are reflected in the tab bar
    public var mapVC: UIViewController!
    public var favoritesVC: UIViewController!
    public var chatVC: UIViewController!
    public var profileVC: UIViewController!

    //Array for the VCs above
    public var viewControllers: [UIViewController]!

    //Index of the selected button determend by their tags
    public var selectedIndex: Int = 0

    @IBOutlet weak var loadingLogo: UIImageView!

    override public func viewDidLoad() {
        //Populating viewControllers array with
        //initiated VCs in Main storyboard
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        mapVC = storyboard.instantiateViewController(withIdentifier: "MapVC")
        favoritesVC = storyboard.instantiateViewController(withIdentifier: "FavoritesVC")
        chatVC = storyboard.instantiateViewController(withIdentifier: "ChatVC")
        profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileVC")
        viewControllers = [mapVC, favoritesVC, chatVC, profileVC]

        //Custom tab bar + buttons visual properties
            customTabBarContainer.layer.cornerRadius = customTabBarContainer.frame.height / 2
            customTabBarContainer.layer.shadowColor = UIColor.darkGray.cgColor
            customTabBarContainer.layer.shadowOffset = CGSize.zero
            customTabBarContainer.layer.shadowRadius = 10
            customTabBarContainer.layer.shadowOpacity = 0.9
            tabBarButtons[0].imageView?.contentMode = .scaleAspectFit
            tabBarButtons[1].imageView?.contentMode = .scaleAspectFit
            tabBarButtons[2].imageView?.contentMode = .scaleAspectFit
            tabBarButtons[3].imageView?.contentMode = .scaleAspectFit

    }


    override public func viewDidAppear(_ animated: Bool) {
        loadingLogo.popOut()
        //Loads the initial VC
        contentView.addSubview(mapVC.view)
        mapVC.view.frame = self.view.frame
        mapVC.didMove(toParentViewController: self)
        customTabBarContainer.isHidden = false
        //Selects the inital home button
        tabBarButtons[0].isSelected = true
    }


    @IBAction func didTabButton(_ sender: UIButton) {

        //Keeps a track of which bar button is selected
        let previousIndex = selectedIndex
        selectedIndex = sender.tag

        //Deselects the previous bar button
        tabBarButtons[previousIndex].isSelected = false

        //Removes the previous VC
        let previousVC = viewControllers[previousIndex]
        previousVC.view.removeFromSuperview()
        previousVC.removeFromParentViewController()

        print("switced to \(viewControllers[selectedIndex])")

        //Selects the tapped bar button
        tabBarButtons[selectedIndex].isSelected = true
        tabBarButtons[selectedIndex].popIn()

        //Brings up the selected VC
        let nextVC = viewControllers[selectedIndex]
        contentView.addSubview(nextVC.view)
        nextVC.view.frame = self.view.frame
        nextVC.didMove(toParentViewController: self)

    }
}

这是我试图用来从MapVC的子级中操作customTabBarContainer的代码:

    UIView.animate(withDuration: 0.4, animations: {

        let root = self.parent?.parent as! RootVC
        root.customTabBarContainer.frame.origin.y -= root.customTabBarContainer.frame.height

    }, completion: nil)

为什么要尝试访问父级的父级?

self.parent?.parent as RootVC

假设您正在使用像这样的扩展来查找您的parentVC:

extension UIView {
    var parentViewController: UIViewController? {
        var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if parentResponder is UIViewController {
                return parentResponder as! UIViewController!
            }
        }
        return nil
    }
} 

您应该可以通过以下方式访问父项

let root = self.parentViewController as! RootVC

我想出了一个答案,以防万一其他人遇到类似的问题。 它不会带您到VC的直接父级,而是带到其最远的祖先,在这种情况下,这可以解决我的特定问题。

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let rootVC = appDelegate.window?.rootViewController as! RootVC
    rootVC.customTabBarContainer.isHidden = true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM