简体   繁体   English

使用 ViewController 中的 CGFloat 值在 Swift 中的 TabBarController 中移动视图

[英]Using a CGFloat value from ViewController to move a view in TabBarController in Swift

I'm so close to just throw my laptop away.我差点把我的笔记本电脑扔掉了。 For 3 days, again, 3 days I'm looking for this specific piece of code and I cannot seem to find out how. 3 天,又是 3 天,我一直在寻找这段特定的代码,但我似乎无法找到方法。 My project became a whole commented-out-code graveyard.我的项目变成了一个完整的注释掉代码墓地。 I would give anything for an answer.我会给出任何答案。 Please help me.请帮我。

What I want: My ViewController has a gestureRecognizer that gets me the value eg.我想要什么:我的ViewController有一个gestureRecognizer ,可以让我获得价值,例如。 var fingerPos of the current finger position.当前手指位置的var fingerPos Simple example:简单的例子:

 @objc func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {
        fingerPos = sender.location(in: view?.window)
}

I want to get the fingerPos position and pass it to the TabBarController .我想获取fingerPos位置并将其传递给TabBarController That value should then be used to move the TabBarController .然后应该使用该值来移动TabBarController For example self.view.frame.origin.y = fingerPos.y .例如self.view.frame.origin.y = fingerPos.y The movement/update of the value must be continuous.值的移动/更新必须是连续的。

What I tried: NSNotificationCenter , didSet , Structs in combination with didSet , Various types of function passing eg.我尝试过的: NSNotificationCenterdidSet ,与didSet结合的Structs ,各种类型的函数传递,例如。

//Function in ViewController

func IBegYouGiveMeTheValueFunction() -> CGFloat {
 return fingerPos
}


//In the TabBarController

let vc = ViewController()
self.view.frame.origin.y = vc.IBegYouGiveMeTheValueFunction()

that resulted in crashes because the "Value was found nil" even though it had a value, Adding the TabBarController as a child which resulted in a white screen on the simulator and fan spinning, Values written out of any classes (public), Trying to set the position of the TabBarController in the ViewController , literally anything I found online.这导致崩溃,因为“值被发现为零”,即使它有一个值,将TabBarController添加为子项,导致模拟器上出现白屏和风扇旋转,从任何类(公共)写出的值,试图在ViewController设置TabBarController的位置,实际上是我在网上找到的任何内容。

Can anyone help me with this?谁能帮我这个? I would be really thankful!我真的很感激!

You do not need a custom TabBarController class.您不需要自定义 TabBarController 类。

The view controllers in each tab can be accessed via the tab bar controllers .viewControllers array, so you can call funds or set properties directly.每个选项卡中的视图控制器可以通过选项卡栏控制器.viewControllers数组访问,因此您可以直接调用基金或设置属性。

  • In your first tab view controller, track the finger position with the drag gesture.在您的第一个选项卡视图控制器中,使用拖动手势跟踪手指位置。
  • When it changes, get a reference to the view controller in the second tab.当它发生变化时,在第二个选项卡中获取对视图控制器的引用。
  • Call a func in that vc, which will, for example, update the position of a subView.在该 vc 中调用一个 func,例如,它将更新子视图的位置。

Here is a complete, simple example.这是一个完整的简单示例。 Just add a UITabBarController to a Storyboard, and assign the class of the two default tab controllers to MyFirstViewController and MySecondViewController included in the following code (you don't need to add anything else... no IBOutlet or IBAction needed):只需将一个UITabBarController添加到 Storyboard,并将两个默认选项卡控制器的类分配给以下代码中包含的MyFirstViewControllerMySecondViewController (您不需要添加任何其他内容...不需要IBOutletIBAction ):

class MyFirstViewController: UIViewController {

    let statusLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .red
        v.textColor = .white
        v.textAlignment = .center
        v.numberOfLines = 0
        v.text = "Tracking" + "\n\n" + "Drag your finger in view..."
        return v
    }()

    var topConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(statusLabel)

        topConstraint = statusLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 200.0)

        NSLayoutConstraint.activate([
            topConstraint,
            statusLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            statusLabel.widthAnchor.constraint(equalToConstant: 240.0),
        ])

        let pg = UIPanGestureRecognizer(target: self, action: #selector(panGestureRecognizerHandler(_:)))
        view.addGestureRecognizer(pg)

    }

    @objc func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {

        let fingerPos = sender.location(in: view)
        var strTmp = "Tracking Y: \(fingerPos.y)"
        topConstraint.constant = fingerPos.y

        guard let tb = self.tabBarController else {
            strTmp += "\n\n" + "Must be in a tabBarController!"
            statusLabel.text = strTmp
            return
        }

        guard let vControllers = tb.viewControllers else {
            strTmp += "\n\n" + "Could not get viewControllers from tabBarController!"
            statusLabel.text = strTmp
            return
        }

        guard vControllers.count > 1, let secondVC = vControllers[1] as? MySecondViewController else {
            strTmp += "\n\n" + "VC in second tab is not MySecondViewController!"
            statusLabel.text = strTmp
            return
        }

        // in case we have not yet navigated to the second tab
        secondVC.loadViewIfNeeded()

        // tell SecondVC to update the Y position
        secondVC.updateYPosition(at: fingerPos.y)

        strTmp += "\n\n" + "Sent!"

        statusLabel.text = strTmp

    }

}

class MySecondViewController: UIViewController {

    let statusLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .blue
        v.textColor = .white
        v.textAlignment = .center
        v.numberOfLines = 0
        v.text = "Have not received" + "\n\n" + "a Y update yet..."
        return v
    }()

    var topConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(statusLabel)

        topConstraint = statusLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 200.0)

        NSLayoutConstraint.activate([
            topConstraint,
            statusLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            statusLabel.widthAnchor.constraint(equalToConstant: 240.0),
        ])

    }

    func updateYPosition(at y: CGFloat) -> Void {
        topConstraint.constant = y
        statusLabel.text = "Top contstraint constant:" + "\n\n" + "\(y)"
    }

}

When you run this, the first thing you see is the first tab:当您运行它时,您首先看到的是第一个选项卡:

在此处输入图片说明

As you tap-and-drag your finger, the top of the red label will follow:当您点击并拖动手指时,红色标签的顶部将跟随:

在此处输入图片说明

When you select the second tab, you will see a blue label with its top at the same Y position where you dragged the red label:当您选择第二个选项卡时,您将看到一个蓝色标签,其顶部位于您拖动红色标签的相同 Y 位置:

在此处输入图片说明

暂无
暂无

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

相关问题 如何在没有 segue 的情况下从 Viewcontroller 移动到 TabBarController - How to move from Viewcontroller to TabBarController without segue Swift-TabBarController-> ViewController-> NavigationController - Swift - TabBarController -> ViewController -> NavigationController 在Swift中将图像从ViewController传递到TabBarController的ViewController子级 - Pass image from ViewController to ViewController child of TabBarController in Swift iOS Swift 5 在 TabBarController 的视图控制器中使用来自云数据库的数据 - iOS Swift 5 Using Data from Cloud Database in View Controllers in TabBarController 使用我的ViewController作为TabBarController - using my ViewController as a TabBarController Swift-如何使用从ViewController到特定TabBarController的按钮进行搜索 - Swift - How do I segue with a button from ViewController to a specific TabBarController 如何将数据从 XIB ViewController 传递到 iOS Swift 中的 TabBarController - How to pass data from XIB ViewController to TabBarController in iOS Swift 在Swift中以编程方式从主TabBarController转到嵌套的ViewController - Go to a nested ViewController from a main TabBarController programmatically in Swift 从View Controller调用TabBarController委托-Swift - Call TabBarController delegate from View Controller - Swift iOS-使用xib中的tabBarController将数据从一个viewController传递到另一个 - iOS - Pass Data from one viewController to another using tabBarController in xib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM