无法点击作为ImageView子视图的按钮

时间:2020-03-25 18:49:33

标签: ios swift xcode uibutton autolayout

ViewController-> View-> ScrollView-> ImageView->按钮

我有一些要添加为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
    }

}

0 个答案:

没有答案