我正在尝试检测 UIView 中 BezierPaths 内的 Tap,并发现许多对 containsPoint 方法的引用。但是我似乎无法找到如何从我的 ViewController 中实际引用 BezierPaths。
我已设置:
class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 69, height: 107), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct])
{
let myPath = UIBezierPath()
myPath.move(to: CGPoint(x: 32.24, y: 8.61))
myPath.addLine(to: CGPoint(x: 31.99, y: 8.29))
myPath.addLine(to: CGPoint(x: 31.78, y: 8.19))
myPath.close()
}
贝塞尔曲线是在此函数中绘制的,调用者:
override func draw(_ rect: CGRect)
在主 ViewController 中,我有以下函数来检测 UIView 上的点击:
@objc func SATap(sender: UITapGestureRecognizer)
{
let location = sender.location(in: self.SAView)
// how do I call containsPoint from here?
}
如何从这里调用 containsPoint?
bezierPaths 在运行时正确绘制。
最佳答案
由于路径是在单独的类中的类函数中创建的,因此我无法将它们直接保存到 View 中。
我通过将 UIBezierPaths 放入字典中然后返回字典来解决这个问题。数组也可以,但这样我可以轻松访问特定路径。
class func drawSA(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 71, height: 120), resizing: ResizingBehavior = .aspectFit, SACountries: [String: ViewController.CountryStruct]) -> ([String: UIBezierPath])
{
var Paths = [String: UIBezierPath]()
let myPath = UIBezierPath()
myPath.move(to: CGPoint(x: 32.24, y: 8.61))
myPath.addLine(to: CGPoint(x: 31.99, y: 8.29))
myPath.addLine(to: CGPoint(x: 31.78, y: 8.19))
myPath.close()
Paths["mP"] = myPath
let myPath2 = UIBezierPath()
myPath2.move(to: CGPoint(x: 32.24, y: 8.61))
myPath2.addLine(to: CGPoint(x: 31.99, y: 8.29))
myPath2.addLine(to: CGPoint(x: 31.78, y: 8.19))
myPath2.close()
Paths["mP2"] = myPath2
return Paths
}
然后,我使用 View.addLayer
在 View 中创建图层,并在使用字典上的 For In 循环创建图层时向每个图层添加 Layer.name
属性:
for path in Paths.keys.sorted()
{
self.layer.addSublayer(CreateLayer(path)) // The Function simply creates a CAShapeLayer
}
然后我在手势功能中使用了字典:
@objc func ATap(sender: UITapGestureRecognizer)
{
let location = sender.location(in: self.SAView)
// You need to Scale Location if your CAShapeLayers are Scaled
for path in SAView.Paths
{
let value = path.value
value.contains(location)
if value.contains(location)
{
for (index, layer) in SAView.layer.sublayers!.enumerated()
{
if layer.name == path.key
{
// Do something when specific layer is Tapped
}
}
}
}
}
很可能有更好的方法来做到这一点,但它们都有效并且性能良好。
关于ios - 如何在 UIView 中引用 BezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47023374/