当我触摸任何单元格时,它应该被调用,但是当我在任何单元格上触摸5-6秒时,它就会被调用。您可以在上图中看到。
我分别创建了相同的下拉按钮,并且该按钮正常工作。但是当我将其包含在项目中时,它将无法正常工作。
UIViewController
import UIKit
class Registration: UIViewController {
var genderDropDown: DropDownButton = {
let dropDown = DropDownButton(frame: CGRect(x: 0,y: 0,width: 0,height: 0))
dropDown.setTitle("Gender",for: .normal)
dropDown.translatesAutoresizingMaskintoConstraints = false
dropDown.backgroundColor = setColor(rgbValue: AppColor.pink)
dropDown.setTitleColor(.white,for: .normal)
return dropDown
}()
override func viewDidLoad() {
super.viewDidLoad()
registrationContainer.addSubview(genderDropDown)
NSLayoutConstraint.activate([
genderDropDown.topAnchor.constraint(equalTo: emailTF.bottomAnchor,constant: 30),genderDropDown.leftAnchor.constraint(equalTo: registrationContainer.leftAnchor,constant: 20),genderDropDown.heightAnchor.constraint(equalToConstant: 50),genderDropDown.widthAnchor.constraint(equalToConstant: 100)
])
}
}
DropDownButton
class DropDownButton: UIButton,DropDownProtocol {
func dropDownpressed(string: String) {
self.setTitle(string,for: .normal)
self.dismissDropDown()
}
var dropDown = DropDownView()
var height = NSLayoutConstraint()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .darkGray
dropDown = DropDownView.init(frame: CGRect(x: 0,height: 0))
dropDown.delegate = self
dropDown.translatesAutoresizingMaskintoConstraints = false
}
override func didmoveToSuperview() {
self.superview?.addSubview(dropDown)
self.superview?.bringSubviewToFront(dropDown)
dropDown.centerXAnchor.constraint(equalTo: self.centerXAnchor).isactive = true
dropDown.topAnchor.constraint(equalTo: self.bottomAnchor).isactive = true
dropDown.widthAnchor.constraint(equalTo: self.widthAnchor).isactive = true
height = dropDown.heightAnchor.constraint(equalToConstant: 0)
}
var isOpen = false
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
if isOpen == false{
isOpen = true
NSLayoutConstraint.deactivate([self.height])
if self.dropDown.tableView.contentSize.height > 150{
self.height.constant = 150
}
else {
self.height.constant = self.dropDown.tableView.contentSize.height
}
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5,delay: 0,usingSpringWithDamping: 0.5,initialSpringVelocity: 0.5,options: .curveeaseinout,animations: {
self.dropDown.layoutIfNeeded()
self.dropDown.center.y += self.dropDown.frame.height / 2
},completion: nil)
} else {
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5,animations: {
self.dropDown.center.y -= self.dropDown.frame.height / 2
self.dropDown.layoutIfNeeded()
},completion: nil)
}
}
func dismissDropDown(){
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5,animations: {
self.dropDown.center.y -= self.dropDown.frame.height / 2
self.dropDown.layoutIfNeeded()
},completion: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
DropDownView
class DropDownView: UIView,UITableViewDelegate,UITableViewDataSource{
let items = ["Male","Female"]
let tableView = UITableView()
var delegate: DropDownProtocol!
override init(frame: CGRect) {
super.init(frame: frame)
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskintoConstraints = false
self.addSubview(tableView)
tableView.backgroundColor = .darkGray
tableView.separatorStyle = .none
tableView.allowsSelection = true
tableView.delaysContentTouches = false
tableView.register(UITableViewCell.self,forCellReuseIdentifier: "cellId")
tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isactive = true
tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isactive = true
tableView.topAnchor.constraint(equalTo: self.topAnchor).isactive = true
tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isactive = true
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return items.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
DispatchQueue.main.async {
self.delegate.dropDownpressed(string: self.items[indexPath.row])
self.tableView.deselectRow(at: indexPath,animated: true)
}
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId",for: indexPath)
cell.textLabel?.text = items[indexPath.row]
cell.textLabel?.textColor = .white
cell.backgroundColor = setColor(rgbValue: AppColor.pink)
return cell
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}