point和hitTest之间有什么区别,我如何使用hitTest?

时间:2019-03-03 15:35:12

标签: ios swift swift4.2

我想在我的scrollView中添加触摸区域,并且我在容器视图中有scrollView。 scrollView的页面小于该容器。我在互联网上发现了很多东西,在Swift 4.2中如何使用,但是没有人一步一步地说明如何使用hitTest。当我执行UIView类型的子类时,我该怎么办?在具有scroolView或containerView的类中之后如何使用该类?

谢谢。

这是代码。

import UIKit

struct scrollViewDataStruct {
    let title: String?
    let image: UIImage?
}

class PaymentsOptionsController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    var scrollViewData = [scrollViewDataStruct]()

    override func viewDidLoad() {
    super.viewDidLoad()
    self.scrollView.delegate = self

    scrollViewData = [scrollViewDataStruct.init(title: "First", image:  #imageLiteral(resourceName: "amex")),scrollViewDataStruct.init(title: "Second", image:  #imageLiteral(resourceName: "chase-debit-card_front")),scrollViewDataStruct.init(title: "Third", image:  #imageLiteral(resourceName: "amex"))]
    scrollView.contentSize.width = self.scrollView.frame.width * (CGFloat(scrollViewData.count) + 1)
    var i: CGFloat = 0
    var j: CGFloat = 1
    let halfInterSpacing: CGFloat = 10.5
    let viewWidth : CGFloat = 305
    let viewOptions = PaymentMenuViewController(frame: CGRect(x: (halfInterSpacing * j) + viewWidth * i, y: 0, width :  viewWidth, height: self.scrollView.frame.height))
    self.scrollView.addSubview(viewOptions)
    i += 1
    j += 2
    for data in scrollViewData{
        let view = CustomView(frame: CGRect(x: (halfInterSpacing * j) + viewWidth * i, y: 0, width :  viewWidth, height: self.scrollView.frame.height))
        view.imageView.image = data.image
        self.scrollView.addSubview(view)

        i += 1
        j += 2
    }
    self.scrollView.clipsToBounds = false
    }


    class CustomView: UIView {   
        let imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = UIColor.white
        imageView.contentMode = .scaleAspectFit
        return imageView
        }()

        override init(frame: CGRect) {
            super.init(frame: frame)
            self.addSubview(imageView)
            imageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
            imageView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
            imageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        }

        required init?(coder aDecoder: NSCoder) {
             fatalError("aaa")
        }
    }   
}


class PassThruScrollView: UIScrollView {

    var passThruViewRef: UIView?

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool    {

    return point.y > passThruViewRef?.frame.height ?? 0

    }

}

Imagen from my stroyBoard

1 个答案:

答案 0 :(得分:1)

如果要让滚动视图的hitTest识别滚动视图之外的手势,则需要convert the coordinate到当前视图的坐标系:

class CustomScrollView: UIScrollView {
    var referenceView: UIView?   // if you leave this `nil`, it will assume `superview`

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = referenceView ?? superview!

        let frame = convert(view.bounds, from: view)
        return frame.contains(point) ? self : nil
    }
}

FWIW,此功能在设备上可以正常运行,但在模拟器上的行为异常,因此,后果自负。

但是下面,我在滚动视图外部的灰色区域中进行手势滚动:

enter image description here