gpt4 book ai didi

ios - 容器 View 中的按钮在第二个 View 中不起作用

转载 作者:行者123 更新时间:2023-11-30 12:04:15 26 4
gpt4 key购买 nike

所以,我有 2 个 View ,中间有一个按钮。该按钮受限于中心 View 。当我滑动到另一个 View 时,按钮移动到另一个 View 上的第二个位置(可见)。我的问题是我无法从其他 View 访问该按钮的功能,因此该按钮不起作用。它是用 contentView 创建的:

enter image description here

这是我的代码:

    import UIKit

class MainVC: UIViewController {

let setupMainVC = SetupMainVC()

var rightViewXConstant : NSLayoutConstraint!
var leftViewXConstant : NSLayoutConstraint!
var arrowLeftXConstant : NSLayoutConstraint!
var centerViewXConstant : NSLayoutConstraint!

var rightViewOpen = false
var leftViewOpen = false

var windowSize = CGFloat()

override func viewDidLoad() {
super.viewDidLoad()

let window = UIWindow.init(frame: UIScreen.main.bounds)
window.makeKeyAndVisible()

view.backgroundColor = UIColor.clear

setupMainVC.mainVC = self
setupMainVC.setupViews()

windowSize = window.frame.size.width

setupLeftViewController()
setupCenterViewController()


navigationItem.title = "Center View"

self.setupMainVC.arrowButtonLeft.transform = CGAffineTransform(rotationAngle: 0)
arrowLeftXConstant.constant = 20

print(windowSize)

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(leftButAction))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(rightButAction))
setupMainVC.arrowButtonLeft.addTarget(self, action: #selector(leftButAction), for: .touchUpInside)

NotificationCenter.default.addObserver(self, selector: #selector(leftButAction), name: NSNotification.Name("leftButtonAction"), object: nil)
}

func leftButAction() {
print("Left Action")

if leftViewOpen {
leftViewOpen = false
centerViewXConstant.constant = 0
leftViewXConstant.constant = 0
self.setupMainVC.arrowButtonLeft.transform = CGAffineTransform(rotationAngle: 0)
arrowLeftXConstant.constant = 20
navigationItem.title = "Center View"
print(false)
} else {
leftViewOpen = true
centerViewXConstant.constant = windowSize
leftViewXConstant.constant = windowSize
self.setupMainVC.arrowButtonLeft.transform = CGAffineTransform(rotationAngle: -179)
arrowLeftXConstant.constant = -20
navigationItem.title = "Left View"
print(true)
}

UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}

}

func rightButAction() {
print("Right Action")

if rightViewOpen {
rightViewOpen = false
print(false)
} else {
rightViewOpen = true
print(true)
}

UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}

func center() {
let center = CenterVC()
addChildViewController(center)
center.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(center.view)

centerViewXConstant = center.view.centerXAnchor.constraint(equalTo: center.view.centerXAnchor, constant: 0)
centerViewXConstant.isActive = true
center.view.centerYAnchor.constraint(equalTo: center.view.centerYAnchor).isActive = true
center.view.heightAnchor.constraint(equalTo: center.view.heightAnchor).isActive = true
center.view.widthAnchor.constraint(equalTo: center.view.widthAnchor).isActive = true
center.view.backgroundColor = .blue

center.didMove(toParentViewController: self)
}

//MARK: Center View
func setupCenterViewController() {
let controller = CenterVC()

addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)
controller.view.addSubview(setupMainVC.roundViewOnLeftView)
controller.view.addSubview(setupMainVC.arrowButtonLeft)

centerViewXConstant = controller.view.leftAnchor.constraint(equalTo: setupMainVC.leftView.rightAnchor, constant: 0)
centerViewXConstant.isActive = true

controller.view.centerYAnchor.constraint(equalTo: setupMainVC.leftView.centerYAnchor).isActive = true
controller.view.heightAnchor.constraint(equalTo: setupMainVC.leftView.heightAnchor).isActive = true
controller.view.widthAnchor.constraint(equalTo: setupMainVC.leftView.widthAnchor).isActive = true

setupMainVC.roundViewOnLeftView.centerXAnchor.constraint(equalTo: controller.view.leftAnchor).isActive = true
setupMainVC.roundViewOnLeftView.centerYAnchor.constraint(equalTo: controller.view.centerYAnchor).isActive = true
setupMainVC.roundViewOnLeftView.heightAnchor.constraint(equalToConstant: 100).isActive = true
setupMainVC.roundViewOnLeftView.widthAnchor.constraint(equalToConstant: 100).isActive = true

arrowLeftXConstant = setupMainVC.arrowButtonLeft.centerXAnchor.constraint(equalTo: setupMainVC.roundViewOnLeftView.centerXAnchor, constant: 20)
arrowLeftXConstant.isActive = true

setupMainVC.arrowButtonLeft.centerYAnchor.constraint(equalTo: setupMainVC.roundViewOnLeftView.centerYAnchor).isActive = true
setupMainVC.arrowButtonLeft.heightAnchor.constraint(equalToConstant: 100).isActive = true
setupMainVC.arrowButtonLeft.widthAnchor.constraint(equalToConstant: 100).isActive = true

controller.didMove(toParentViewController: self)
}

//MARK: Left View
func setupLeftViewController() {
let controller = LeftVC()
//let navBar: UINavigationController = UINavigationController(rootViewController: controller)
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(controller.view)

leftViewXConstant = controller.view.rightAnchor.constraint(equalTo: setupMainVC.centerView.leftAnchor, constant: 0)
leftViewXConstant.isActive = true

controller.view.centerYAnchor.constraint(equalTo: setupMainVC.centerView.centerYAnchor).isActive = true
controller.view.heightAnchor.constraint(equalTo: setupMainVC.centerView.heightAnchor).isActive = true
controller.view.widthAnchor.constraint(equalTo: setupMainVC.centerView.widthAnchor).isActive = true

controller.didMove(toParentViewController: self)
}
}

最佳答案

设法解决问题。小事总是大麻烦。由于这部分功能,它不起作用:

controller.view.addSubview(setupMainVC.roundViewOnLeftView)
controller.view.addSubview(setupMainVC.arrowButtonLeft)

要使按钮正常工作,它需要是 MainView(容器)的 subview ,因此它将位于同一 View 层次结构中,如下所示:

view.addSubview(setupMainVC.roundViewOnLeftView)
view.addSubview(setupMainVC.arrowButtonLeft)

关于ios - 容器 View 中的按钮在第二个 View 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46857296/

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