Answer the question
In order to leave comments, you need to log in
Why doesn't UITapGestureRecognizer fire?
Hello!
There is the following design element:
Accordingly, when the user sees the window for the first time, none of the options is selected:
The last option is implemented through a view that has a stack that will contain "Man" and "Woman". I made circles through two views (leftOuterView, leftInnerView). I didn’t go by the buttons, because I want to make a beautiful animation when choosing.
I add UITapGestureRecognizer, but it does not work (does not call the selector). isUserInteractionEnabled seems to be enabled in all internal elements (highlighted with brands). Please help, why is UITapGestureRecognizer not working?
Here is the final code:
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)
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question