我有2个视图,每个视图都有自己的UITapGestureRecognizer。当视图在首次加载时出现时,轻击手势功能可以完美工作。但是当视图没有出现在第一个负载上时,这意味着我必须先滚动它才能查看视图,突然间,轻击手势不起作用。这怎么发生的?这是我的代码
import UIKit
class EditorTwitterViewController: UIViewController {
@IBOutlet weak var mainScrollView: UIScrollView!
@IBOutlet weak var contentTitleLabel: UILabel!
@IBOutlet weak var dateActivityLabel: UILabel!
@IBOutlet weak var statusViewHeight: NSLayoutConstraint!
@IBOutlet weak var photosViewHeight: NSLayoutConstraint!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var approveView: UIView!
@IBOutlet weak var approveImageView: UIImageView!
@IBOutlet weak var approveLabel: UILabel!
@IBOutlet weak var rejectView: UIView!
@IBOutlet weak var rejectImageView: UIImageView!
@IBOutlet weak var rejectLabel: UILabel!
@IBOutlet weak var noteViewHeight: NSLayoutConstraint!
@IBOutlet weak var noteTextView: UITextView!
var hasStatus: Bool?
var numberOfPhotos = 5
var numberOfRows = 3
override func viewDidLoad() {
super.viewDidLoad()
hasStatus = false
noteTextView.placeholder = "Note"
noteIsVisible(tag: 0)
setupTableView()
setupStatusView()
self.collectionView.register(UINib(nibName: "ImagesCell", bundle: nil), forCellWithReuseIdentifier: "ImagesCell")
self.collectionView.reloadData()
self.setupApprovalView()
}
override func viewDidAppear(_ animated: Bool) {
self.setupMainScrollView()
}
private func setupTableView() {
self.tableView.register(UINib(nibName: "TwitterEditorCell", bundle: nil), forCellReuseIdentifier: "TwitterEditorCell")
self.tableView.rowHeight = 250
self.tableView.estimatedRowHeight = UITableViewAutomaticDimension
}
private func setupMainScrollView() {
let photosViewHeight = CGFloat(54 * numberOfPhotos)
let scrollViewHeight = 800 + photosViewHeight
self.photosViewHeight.constant = photosViewHeight
self.mainScrollView.contentSize.height = scrollViewHeight
self.mainScrollView.keyboardDismissMode = .onDrag
self.view.setNeedsLayout()
self.view.setNeedsUpdateConstraints()
}
private func setupStatusView() {
if hasStatus! {
self.statusViewHeight.constant = 44
} else {
self.statusViewHeight.constant = 0
}
self.view.setNeedsUpdateConstraints()
}
private func setupApprovalView() {
let approved = UITapGestureRecognizer(target: self, action: #selector(changeStatus(sender:)))
let rejected = UITapGestureRecognizer(target: self, action: #selector(changeStatus(sender:)))
approveView.addGestureRecognizer(approved)
rejectView.addGestureRecognizer(rejected)
}
@objc private func changeStatus(sender: UIGestureRecognizer) {
print(sender.view?.tag ?? "")
if sender.view?.tag == 0 {
approveImageView.image = UIImage(named: "approvegreen")
approveLabel.textColor = UIColor.greenWithHex
rejectImageView.image = UIImage(named: "rejected")
rejectLabel.textColor = UIColor.blackWithHex
} else if sender.view?.tag == 1 {
approveImageView.image = UIImage(named: "approve")
approveLabel.textColor = UIColor.blackWithHex
rejectImageView.image = UIImage(named: "rejectedred")
rejectLabel.textColor = UIColor.redWithHex
}
noteIsVisible(tag: sender.view!.tag)
}
private func noteIsVisible(tag: Int) {
if tag == 0 {
noteViewHeight.constant = 0
} else if tag == 1 {
noteViewHeight.constant = 150
}
self.view.setNeedsUpdateConstraints()
}
}
其中mainScrollView是父视图的主滚动视图,setupApprovalView()是为每个视图初始化UITapGesture的函数,而changeStatus()是在单击该视图时发生的函数