ViewController-> View-> ScrollView-> ImageView-> Buttons
У меня есть кнопки, которые я добавляю как подпредставления моего imageView, и я не могу нажать ни одну из них. Я попытался добавить действие с событиями .touchUpInside
и .touchDown
. У меня также есть точка останова внутри функции touchesBegan
, и она никогда не активируется. Я напечатал границы imageView, а также границы некоторых кнопок, и, насколько мне известно, как работают границы, они должны быть внутри границ imageView. Почему мои кнопки не запускаются, когда я нажимаю на них?
class myVC: UIViewController {
var data: Data
// MARK: UIComponents
lazy var scrollView: PassThroughScrollView = { //These PassThrough Views just return false on a hitTest, Code for them is below
let _scrollView = PassThroughScrollView(frame: .zero)
_scrollView.translatesAutoresizingMaskIntoConstraints = false
_scrollView.clipsToBounds = false
_scrollView.isUserInteractionEnabled = true
return _scrollView
}()
lazy var imageView: PassThroughImageView = {
let _imageView = PassThroughImageView(frame: .zero)
_imageView.translatesAutoresizingMaskIntoConstraints = false
_imageView.contentMode = .scaleAspectFit
_imageView.isUserInteractionEnabled = true
return _imageView
}()
init(dat: data) {
super.init(nibName: nil, bundle: nil)
self.data = dat
self.imageView.sd_setImage(with: URL(string: UIImage(named: "img"), completed: nil)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
// MARK: Setup
private func setup() {
let viewController = self
let interactor = PlaySectionInteractor()
let presenter = PlaySectionPresenter()
let router = PlaySectionRouter()
viewController.interactor = interactor
viewController.router = router
interactor.presenter = presenter
presenter.viewController = viewController
router.viewController = viewController
router.dataStore = interactor
}
private func setupConstraints() {
view.addSubview(scrollView)
scrollView.addSubview(imageView)
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
])
setupHotSpots()
}
private func setupViews() {
}
private func setupButtons() {
for s in data!.ButtonPoints! {
let button = customButton(point: point)
button.translatesAutoresizingMaskIntoConstraints = false
imageView.addSubview(button)
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
NSLayoutConstraint.activate([
button.widthAnchor.constraint(equalToConstant: sizeConstants.hotspotSize),
button.heightAnchor.constraint(equalToConstant: sizeConstants.hotspotSize),
button.centerXAnchor.constraint(equalTo: imageView.leadingAnchor, constant: CGFloat(s.x!) * 1920 / 100),
button.centerYAnchor.constraint(equalTo: imageView.topAnchor, constant: CGFloat(s.y!) * 1080 / 100),
])
button.addTarget(self, action: #selector(tapButton(sender:)), for: .touchUpInside) //I Have tried both .touchUpInside and .touchDown
button.isUserInteractionEnabled = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
setup()
setupConstraints()
scrollView.delegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
var touch: UITouch? = touches.first
if touch?.view != detailView {
detailView.isHidden = true
}
super.touchesBegan(touches, with: event)
}
@objc func tapButton(sender: UIButton) {
print("Button Successfully Tapped)
}
}
Пропускать просмотры:
class PassThroughView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
for subview in self.subviews {
let convertedPoint = convert(point, to: subview)
let point = subview.point(inside: convertedPoint, with: event)
if (point) {
return subview
}
}
return nil
}
}
class PassThroughImageView: UIImageView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
for subview in self.subviews {
let convertedPoint = convert(point, to: subview)
let point = subview.point(inside: convertedPoint, with: event)
if (point) {
return subview
}
}
return nil
}
}
class PassThroughScrollView: UIScrollView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
for subview in self.subviews {
let convertedPoint = convert(point, to: subview)
let point = subview.point(inside: convertedPoint, with: event)
if (point) {
return subview
}
}
return nil
}
}