Изображение
, когда я касаюсь любой ячейки, она должна вызываться, но она вызывается, когда я держу касание в любой ячейке в течение 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, y: 0, width: 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, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, 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, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, 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")
}
}