gpt4 book ai didi

ios - 从子 VC 动画父 VC 对象

转载 作者:行者123 更新时间:2023-11-30 11:45:31 24 4
gpt4 key购买 nike

是否可以在 Swift 中对父 VC 的 View 进行动画处理?

我有一个带有 UIView 的根/主 VC,我将其用作某种 UITabBarController,因此我的 4 个主要 VC 的其余部分都是根的子级。

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

每当我打开全屏 subview 时,我希望它通过 Y 轴滑出屏幕,但是我似乎无法访问或操作根 VC 属性,因为它在运行时返回 nil。

下面是自定义标签栏根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

关于ios - 从子 VC 动画父 VC 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48872877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com