.isUserInteractionEnabled
каскады к подпредставлениям. Таким образом, даже если я установлю его в true для подпредставления, если для суперпредставления подпредставления (или его суперпредставления и выше по иерархии) .isUserInteractionEnabled
установлено значение false
, подпредставление не получит сенсорные события.
Итак, первое, что нужно сделать, это проверить .isUserInteractionEnabled
на каждом представлении в иерархии.
В вашем удовольствии c, уберите эту строку:
bannerStackView.isUserInteractionEnabled = true
, затем добавьте эти строк после создания tapGesture
:
print("imageView isUserInteractionEnabled:", imageView.isUserInteractionEnabled)
print("bannerStackView isUserInteractionEnabled:", bannerStackView.isUserInteractionEnabled)
print("bannerView isUserInteractionEnabled:", bannerView.isUserInteractionEnabled)
Так это выглядит так:
func setupBannerStackView() {
bannerStackView = UIStackView()
bannerStackView.axis = .vertical
bannerStackView.alignment = .leading
bannerStackView.distribution = .equalCentering
// remove this line (comment-it out)
// bannerStackView.isUserInteractionEnabled = true
bannerView = UIView()
bannerView.backgroundColor = .blue
bannerStackView.addArrangedSubview(bannerView)
imageView.addSubview(bannerStackView)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onBannerTapped))
bannerView.addGestureRecognizer(tapGesture)
print("view isUserInteractionEnabled:", view.isUserInteractionEnabled)
print("imageView isUserInteractionEnabled:", imageView.isUserInteractionEnabled)
print("bannerStackView isUserInteractionEnabled:", bannerStackView.isUserInteractionEnabled)
print("bannerView isUserInteractionEnabled:", bannerView.isUserInteractionEnabled)
}
Вы должны увидеть этот вывод:
view isUserInteractionEnabled: true
imageView isUserInteractionEnabled: false
bannerStackView isUserInteractionEnabled: true
bannerView isUserInteractionEnabled: true
, и это говорит Вы .... вам просто нужно добавить:
imageView.isUserInteractionEnabled = true