point和hitTest有什么区别以及我如何使用hitTest?

问题描述 投票:-2回答:1

我想为我的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

ios swift swift4.2
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.