问题
I want to create a Bezier curve, and create a collision boundary for my object.
let firstSurfacePoint = CGPoint(x: 30, y: 120)
let secondSurfacePoint = CGPoint(x: 20, y: 200)
let thirdSurfacePoint = CGPoint(x: 200, y: 300)
let center = CGPoint(x: 150, y: 120)
let radius: CGFloat = 120.00
let arcWidth: CGFloat = 20.00
let fourthSurfacePoint = CGPoint(x: 240, y: 300)
func createCollisionPath(collision: UICollisionBehavior) {
let line = UIBezierPath()
line.moveToPoint(firstSurfacePoint)
line.addCurveToPoint(thirdSurfacePoint, controlPoint1: secondSurfacePoint, controlPoint2: secondSurfacePoint)
line.addLineToPoint(fourthSurfacePoint)
let currentPath = CAShapeLayer()
currentPath.path = line.CGPath
currentPath.strokeColor = UIColor.blackColor().CGColor
currentPath.fillColor = UIColor.clearColor().CGColor
currentPath.lineWidth = 1
view.layer.addSublayer(currentPath)
collision.addBoundaryWithIdentifier("curve", forPath: line)
}
the same poor result I get, if I choose addArcWithCente
line.moveToPoint(firstSurfacePoint)
line.addArcWithCenter(center, radius: radius, startAngle: CGFloat(M_PI), endAngle: CGFloat(M_PI/2), clockwise: false)
line.addLineToPoint(fourthSurfacePoint)
In this two tries receive a strange results, that are crucial, as I can't create a proper collision. In the first attempt my object goes through path as if it is a straight line from firstPoint to thirdPoint, as well as in the second
What am I doing wrong?
My figure(without Arc)
My figure(with Arc)
回答1:
currentPath.fillColor = nil
or
currentPath.fillColor = UIColor.clearColor().CGColor
来源:https://stackoverflow.com/questions/42006716/how-can-i-prevent-closing-bezier-path