iOS CALayer 点击识别器
iOS CALayer tap recognizer
我有这样一行:
使用以下代码在 CAShapeLayer 上绘制:
class LineView: UIView {
let rwPath = UIBezierPath()
let rwLayer = CAShapeLayer()
let rwColor = UIColor(red: 11/255.0, green: 86/255.0, blue: 14/255.0, alpha: 1.0)
func setData() {
rwPath.move(to: CGPoint(x: 50.0, y: 50.0))
for i in 1...200 {
rwPath.addLine(to: CGPoint(x: i + 50, y: i + 50))
}
setUpRWLayer()
layer.addSublayer(rwLayer)
}
private func setUpRWLayer() {
rwLayer.path = rwPath.cgPath
rwLayer.fillColor = nil
rwLayer.lineCap = .butt
rwLayer.lineDashPattern = nil
rwLayer.lineDashPhase = 0.0
rwLayer.lineJoin = .miter
rwLayer.lineWidth = 3.0
rwLayer.miterLimit = 10.0
rwLayer.strokeColor = rwColor.cgColor
}
}
我一直在尝试检测该线路上的窃听。我试过使用 UITapGestureRecognizer 并覆盖 touchesBegin:withEvent func,但似乎没有任何效果所以我来这里问。
命中测试代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let point = touches.first?.location(in: self)
if let hitLayer = self.layer.hitTest(point!) as? CAShapeLayer {
if (hitLayer.path?.contains(point!))! {
print("Touched")
}
}
}
您需要为其创建轮廓,因为它可以准确地用于 CAShape 图层上的点击事件
试试这个代码,如果遇到任何问题请告诉我
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let point = touch?.location(in: self.view) else { return }
guard let sublayers = self.view.layer.sublayers else { return }
var caShapelayers : [CAShapeLayer] = []
for layer in sublayers {
if layer is CAShapeLayer{
caShapelayers.append(layer as! CAShapeLayer)
}
}
for layer in caShapelayers {
if let path = layer.path{
let outline = path.copy(strokingWithWidth: 13, lineCap: .square, lineJoin: .round, miterLimit: 0)
let iscontain = outline.contains(point)
if iscontain {
// print(layer)
Singleton.sharedSingleton.showAlert(controller: self, message: "You want to delete this link?", title: "Are you sure?") { (buttonPressed) in
if (buttonPressed == "YES"){
layer.removeFromSuperlayer()
}
}
}
}
}
}
我有这样一行:
使用以下代码在 CAShapeLayer 上绘制:
class LineView: UIView {
let rwPath = UIBezierPath()
let rwLayer = CAShapeLayer()
let rwColor = UIColor(red: 11/255.0, green: 86/255.0, blue: 14/255.0, alpha: 1.0)
func setData() {
rwPath.move(to: CGPoint(x: 50.0, y: 50.0))
for i in 1...200 {
rwPath.addLine(to: CGPoint(x: i + 50, y: i + 50))
}
setUpRWLayer()
layer.addSublayer(rwLayer)
}
private func setUpRWLayer() {
rwLayer.path = rwPath.cgPath
rwLayer.fillColor = nil
rwLayer.lineCap = .butt
rwLayer.lineDashPattern = nil
rwLayer.lineDashPhase = 0.0
rwLayer.lineJoin = .miter
rwLayer.lineWidth = 3.0
rwLayer.miterLimit = 10.0
rwLayer.strokeColor = rwColor.cgColor
}
}
我一直在尝试检测该线路上的窃听。我试过使用 UITapGestureRecognizer 并覆盖 touchesBegin:withEvent func,但似乎没有任何效果所以我来这里问。
命中测试代码:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let point = touches.first?.location(in: self)
if let hitLayer = self.layer.hitTest(point!) as? CAShapeLayer {
if (hitLayer.path?.contains(point!))! {
print("Touched")
}
}
}
您需要为其创建轮廓,因为它可以准确地用于 CAShape 图层上的点击事件
试试这个代码,如果遇到任何问题请告诉我
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let point = touch?.location(in: self.view) else { return }
guard let sublayers = self.view.layer.sublayers else { return }
var caShapelayers : [CAShapeLayer] = []
for layer in sublayers {
if layer is CAShapeLayer{
caShapelayers.append(layer as! CAShapeLayer)
}
}
for layer in caShapelayers {
if let path = layer.path{
let outline = path.copy(strokingWithWidth: 13, lineCap: .square, lineJoin: .round, miterLimit: 0)
let iscontain = outline.contains(point)
if iscontain {
// print(layer)
Singleton.sharedSingleton.showAlert(controller: self, message: "You want to delete this link?", title: "Are you sure?") { (buttonPressed) in
if (buttonPressed == "YES"){
layer.removeFromSuperlayer()
}
}
}
}
}
}