[英]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.我尝试过的:
NSNotificationCenter
, didSet
,与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
数组访问,因此您可以直接调用基金或设置属性。
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,并将两个默认选项卡控制器的类分配给以下代码中包含的MyFirstViewController
和MySecondViewController
(您不需要添加任何其他内容...不需要IBOutlet
或IBAction
):
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.