point 和 hitTest 之间有什么区别以及我如何使用 hitTest?
What is difference between point and hitTest and how i can use hitTest?
我想在我的 scrollView 中添加触摸区域,并且我在容器视图中有 scrollView。 scrollView 的页面少了那个容器。我在互联网上发现了很多如何在 Swift 4.2 中使用,但没有人解释如何逐步使用 hitTest。当我执行 UIView 类型的 subclass 时我会做什么?如何在具有 scroolView 或 containerView 的 class 之后使用此 class?
谢谢。
这是代码。
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
如果想让滚动视图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,这在设备上运行良好,但在模拟器上表现异常,因此使用风险自负。
但下面我在滚动视图外的灰色区域做手势时滚动:
我想在我的 scrollView 中添加触摸区域,并且我在容器视图中有 scrollView。 scrollView 的页面少了那个容器。我在互联网上发现了很多如何在 Swift 4.2 中使用,但没有人解释如何逐步使用 hitTest。当我执行 UIView 类型的 subclass 时我会做什么?如何在具有 scroolView 或 containerView 的 class 之后使用此 class?
谢谢。
这是代码。
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
如果想让滚动视图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,这在设备上运行良好,但在模拟器上表现异常,因此使用风险自负。
但下面我在滚动视图外的灰色区域做手势时滚动: