我的目标是获得可以拖动视图的形状,但是首先我想弄清楚这是否是设置框架的正确方法,以使CAShapeLAyer's
框架不全为零,因此我可以稍后将其与触摸事件一起拖动。
下面是我的课程(DrawOVerlay-DO)中的代码,该代码用于我的UIView
上的Viewcontroller
:
private func drawArc(user c: CGContext) {
c.setLineCap(.round)
c.setLineWidth(35.0)
c.addArc(center: CGPoint(x:187.5,y:200), radius: 90.0, startAngle: -0.5 * .pi, endAngle: (-0.5 * .pi) + (3 / 2 * .pi ), clockwise: false)
c.replacePathWithStrokedPath()
let shape = CAShapeLayer()
shape.frame = bounds
shape.path = c.path
shape.fillColor = UIColor.red.cgColor
shape.strokeColor = UIColor.darkGray.cgColor
shape.lineWidth = 1
print("DO Bounds before:\(String(describing: shape.bounds))")
print("DO Frame before:\(String(describing: shape.frame))")
print("DO BoundingBox:\(String(describing: shape.path?.boundingBox))")
shape.bounds = (shape.path?.boundingBox)!
print("DO Bounds after:\(String(describing: shape.bounds))")
print("DO Frame Width:\(String(describing: shape.frame))")
self.layer.addSublayer(shape)
}
打印语句为我提供了形状bounds
和frame
的正确坐标,但是我对Swift还是陌生的,因此不确定这是否是正确的解决方法。
然后,如何在触摸事件中使用此信息来拖动形状图层而不会闪烁?