2017-02-02 20 views
0

我想创建贝塞尔曲线,并为我的对象创建碰撞边界。如何防止关闭贝塞尔路径

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) 

} 

同样差的结果我得到的,如果让我选择addArcWithCente

line.moveToPoint(firstSurfacePoint) 
line.addArcWithCenter(center, radius: radius, startAngle: CGFloat(M_PI), endAngle: CGFloat(M_PI/2), clockwise: false) 
line.addLineToPoint(fourthSurfacePoint) 

在这两个尝试收到一个奇怪的结果,这是至关重要的,因为我无法创建一个适当的碰撞。在第一次尝试中,我的对象经过路径,就好像它是从第一点到第三点的直线,以及第二次尝试我做错了什么?

My figure(without Arc)

My figure(with Arc)

+0

http://stackoverflow.com/q/40756457/2303865 –

回答

0
currentPath.fillColor = nil 

currentPath.fillColor = UIColor.clearColor().CGColor 
+0

谢谢,这工作,但不是我怀疑。主要问题依然存在。它不会通过这条线创建边界。我的对象仍然经过路径,就好像它是从第一点到第三点的线 – dand1

+0

您确定已将移动对象添加到“UICollectionBehavior”吗?你可以在这里找到一个很好的教程https://www.raywenderlich.com/50197/uikit-dynamics-tutorial如果有帮助 – tgyhlsb

+0

你能找到我的更新请。 – dand1