У меня есть объект UITextView, который отображает сообщение об ошибке. Объект падает с верхней части экрана, как уведомление. По умолчанию он остается неподвижным в течение 5 секунд, а затем снова поднимается над видом, а затем удаляется из суперпредставления. Я добавил UISwipeGestureRecognizer в представление, однако при тестировании присоединенная функция не вызывается. Кто-нибудь может увидеть, что не так?
class ErrorMessage : UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
setupViews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
private func setupViews() {
//ATTRIBUTES & CONSTRAINTS DECLARED HERE ETC...
self.isUserInteractionEnabled = true
self.isMultipleTouchEnabled = true
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(hideError))
swipeUp.direction = .up
self.addGestureRecognizer(swipeUp)
}
@objc func hideError() {
print(true)
}
Вот где я инициализирую код в другом UIView.
let errorMessage = TopMessage()
self.addSubview(errorMessage)
errorMessage.translatesAutoresizingMaskIntoConstraints = false
errorMessage.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 20).isActive = true
errorMessage.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -20).isActive = true
let bottomConstraint = NSLayoutConstraint(item: errorMessage, attribute: .bottom, relatedBy: .equal, toItem: rootVC.view.safeAreaLayoutGuide, attribute: .top, multiplier: 1.0, constant: 0)
NSLayoutConstraint.activate([bottomConstraint])
errorMessage.sizeToFit()
errorMessage.text = "⚠️ Too many reset attempts. Try again later."
//Custom Animation function that I created, just makes constraint changes so the object goes from being hidden above the top of the view, to being constrained to the top safe area.
errorMessage.present(rootVC: rootVC, bottomConstraint: bottomConstraint, relatedItem: rootVC.view.safeAreaLayoutGuide)