我有一个ViewController
,它在加载时具有隐藏视图(我们称它为boxInsideView
)。我希望此boxInsideView
的控制器或.swift
文件在用户单击框时(而不是在视图控制器加载时)触发。就像我点击框时要制作addSubview(boxInsideView
一样。
实例化我的boxInsideView
let cajasInsideView : CajaInsideView = {
let cajasInside = CajaInsideView()
cajasInside.delegate = self
return cajasInside
}()
在viewDidLoad()
上我有以下代码:
scrollView.addSubview(cajasInsideView)
scrollView.addSubview(cajasView)
scrollView.addSubview(collectionView)
cajasView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
cajasView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
cajasView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
cajasView.heightAnchor.constraint(equalToConstant: 150).isActive = true
if !isClosed {
cajasInsideView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
cajasInsideView.autoPinEdge(.top, to: .bottom, of: cajasView, withOffset: -10)
cajasInsideView.heightAnchor.constraint(equalToConstant: 0).isActive = true
cajasInsideView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
}
topConstraint = NSLayoutConstraint(item: collectionView, attribute: .top, relatedBy: .equal, toItem: cajasView, attribute: .bottom, multiplier: 1, constant: 0)
scrollView.addConstraint(topConstraint!)
collectionView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
collectionView.heightAnchor.constraint(equalToConstant: 1000).isActive = true
collectionView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
一旦触发scrollView.addSubview(cajasInsideView)
,就会自动触发我的.swift
文件。
我想做的就是添加此视图,它也是约束条件,动画会增加对该框的单击。
我的openBox()
函数:
@objc func openBox(){
if isClosed {
shareDropDown.dataSource = ["Añadir objetos a esta caja", "Añadir objeto nuevo a esta caja", "Sacar objeto de esta caja"]
shareDropDown.selectionAction = { [unowned self] (index: Int, item: String) in
if index == 1 {
let storyboard = UIStoryboard(name: "Trastero", bundle: nil)
let uploadTrastero = storyboard.instantiateViewController(withIdentifier: "UploadTrasteroViewController") as! UploadTrasteroViewController
uploadTrastero.navigationItem.leftItemsSupplementBackButton = true
self.navigationController?.pushViewController(uploadTrastero, animated: true)
}
if index == 0 {
self.setMoverObjeto()
}
print("Selected item: \(item) at index: \(index)")
}
shareDropDown.width = 250
scrollView.removeConstraint(topConstraint!)
topConstraint = NSLayoutConstraint(item: collectionView, attribute: .top, relatedBy: .equal, toItem: cajasInsideView, attribute: .bottom, multiplier: 1, constant: 0)
UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping:0.7, initialSpringVelocity:0, options: .curveEaseOut, animations: {
self.cajasInsideView.frame = CGRect(x: 0, y: self.cajasView.frame.height - 40, width: self.scrollView.frame.width, height: 360)
self.collectionView.frame = CGRect(x: 0, y: (self.cajasView.frame.height + self.cajasInsideView.frame.height + 40), width: self.view.frame.width, height: 1000)
self.scrollView.addConstraint(self.topConstraint!)
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.headerView.frame.height + self.cajasView.frame.height + self.cajasInsideView.frame.height + self.collectionView.frame.height)
//self.collectionView.autoPinEdge(.top, to: .bottom, of: self.cajasInsideView)
}, completion: { (completion) in
self.isClosed = false
})
}
else{
shareDropDown.dataSource = ["Añadir objeto", "Editar", "Crear nueva caja", "Cambiar ubicación"]
shareDropDown.selectionAction = { [unowned self] (index: Int, item: String) in
if index == 0 {
let storyboard = UIStoryboard(name: "Trastero", bundle: nil)
let uploadTrastero = storyboard.instantiateViewController(withIdentifier: "UploadTrasteroViewController") as! UploadTrasteroViewController
uploadTrastero.navigationItem.leftItemsSupplementBackButton = true
self.navigationController?.pushViewController(uploadTrastero, animated: true)
}
if index == 1 {
self.setEditar()
}
if index == 2 {
self.openPopUpNewBox()
}
if index == 3{
let popupController = PopUpSetDirectionController()
popupController.providesPresentationContextTransitionStyle = true
popupController.definesPresentationContext = true
popupController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext;
popupController.delegate = self
popupController.view.backgroundColor = UIColor.init(white: 0.4, alpha: 0)
self.present(popupController, animated: true, completion: nil)
}
print("Selected item: \(item) at index: \(index)")
}
shareDropDown.width = 170
scrollView.removeConstraint(topConstraint!)
topConstraint = NSLayoutConstraint(item: collectionView, attribute: .top, relatedBy: .equal, toItem: cajasView, attribute: .bottom, multiplier: 1, constant: 0)
esMoverObjeto = false
self.collectionView.reloadData()
UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping:0.7, initialSpringVelocity:0, options: .curveEaseOut, animations: {
self.cajasInsideView.frame = CGRect(x: 0, y: self.cajasView.frame.height - 40, width: self.view.frame.width, height: 0)
self.collectionView.frame = CGRect(x: 0, y: (self.cajasView.frame.height), width: self.view.frame.width, height: 1000)
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.headerView.frame.height + self.cajasView.frame.height + self.cajasInsideView.frame.height + self.collectionView.frame.height)
self.scrollView.addConstraint(self.topConstraint!)
}, completion: { (completion) in
self.isClosed = true
})
}
}
答案 0 :(得分:0)
加载VC后,您将设置第二个view.isHidden = true
的默认值。然后,如果您点击第一个视图,则设置另一个view.isHidden = false
。