Всем привет!
Есть следующий элемент дизайна:
Соответсвенно когда юзер впервые видит окно, то ни один из вариантов не выбран:
Последний вариант реализован через вьюху, в которой есть стек, в котором и будет "Мужчина" и "Женщина". Кружочки сделал через две вьюхи (leftOuterView, leftInnerView). Путем кнопок не пошел, так как хочу сделать красивую анимацию при выборе.
Добавляю UITapGestureRecognizer, но он не работает (не вызывает селектор). isUserInteractionEnabled вроде во всех внутренних элементах включен (выделил марками). Помогите, пожалуйста, почему UITapGestureRecognizer не работает?
Вот в итоге код:
import UIKit
enum Gender{
case male
case female
}
class genderSelectionView: UIView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
func configure(){
// MARK: - isUserInteractionEnabled
self.isUserInteractionEnabled = true
let titleLabel:UILabel = {
let label = UILabel()
label.textColor = UIColor(red:0.68, green:0.74, blue:0.76, alpha:1.0)
label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
label.textAlignment = .left
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false
//label.heightAnchor.constraint(equalToConstant: 15).isActive = true
label.text = "Пол"
return label
}()
addSubview(titleLabel)
titleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15).isActive = true
titleLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
let genderStack = UIStackView()
genderStack.translatesAutoresizingMaskIntoConstraints = false
genderStack.axis = .horizontal
genderStack.distribution = .fillEqually
genderStack.alignment = .leading
let male = genderView()
male.configure(gender: .male)
male.tapRecognizer.addTarget(self, action: #selector(self.maleTapped(recognizer:)))
let female = genderView()
female.configure(gender: .female)
female.tapRecognizer.addTarget(self, action: #selector(self.femaleTapped(recognizer:)))
genderStack.addArrangedSubview(male)
genderStack.addArrangedSubview(female)
addSubview(genderStack)
genderStack.topAnchor.constraint(equalTo: titleLabel.bottomAnchor,constant: 15).isActive = true
genderStack.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15).isActive = true
genderStack.heightAnchor.constraint(equalToConstant: 20).isActive = true
genderStack.trailingAnchor.constraint(equalTo: trailingAnchor,constant: -15).isActive = true
}
@objc func maleTapped(recognizer : UITapGestureRecognizer){
print("male")
}
@objc func femaleTapped(recognizer : UITapGestureRecognizer){
print("female")
}
}
class genderView:UIView{
let leftOuterView:UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.heightAnchor.constraint(equalToConstant: 20).isActive = true
view.widthAnchor.constraint(equalToConstant: 20).isActive = true
view.layer.cornerRadius = 10
view.layer.borderWidth = 1.5
view.layer.borderColor = UIColor(red:0.85, green:0.85, blue:0.85, alpha:1.0).cgColor
// MARK: - isUserInteractionEnabled
view.isUserInteractionEnabled = true
return view
}()
let leftInnerView:UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.heightAnchor.constraint(equalToConstant: 10).isActive = true
view.widthAnchor.constraint(equalToConstant: 10).isActive = true
view.layer.cornerRadius = 5
view.backgroundColor = .white
// MARK: - isUserInteractionEnabled
view.isUserInteractionEnabled = true
return view
}()
let label:UILabel = {
let label = UILabel()
label.textColor = UIColor(red:0.68, green:0.74, blue:0.76, alpha:1.0)
label.font = UIFont.systemFont(ofSize: 16, weight: .medium)
label.textAlignment = .left
label.translatesAutoresizingMaskIntoConstraints = false
// MARK: - isUserInteractionEnabled
label.isUserInteractionEnabled = true
return label
}()
let tapRecognizer = UITapGestureRecognizer()
override init(frame: CGRect) {
super.init(frame:frame)
}
func configure(gender:Gender){
// MARK: - isUserInteractionEnabled
self.isUserInteractionEnabled = true
addSubview(leftOuterView)
leftOuterView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
leftOuterView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
leftOuterView.addSubview(leftInnerView)
leftInnerView.centerYAnchor.constraint(equalTo: leftOuterView.centerYAnchor).isActive = true
leftInnerView.centerXAnchor.constraint(equalTo: leftOuterView.centerXAnchor).isActive = true
addSubview(label)
label.leadingAnchor.constraint(equalTo: leftOuterView.trailingAnchor, constant: 10).isActive = true
label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
switch gender {
case .male:
label.text = "Мужской"
case .female:
label.text = "Женский"
}
addGestureRecognizer(tapRecognizer)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}