2016-09-05 3 views
0

에 'x'를 돌려 : 그래서이 :"경로"CABasicAnimation와 애니메이션 - 나는 체크에 "X"에서 '모프'를 애니메이션을 적용 할 체크 표시

The start path

(이로 변신한다 애니메이션) :

enter image description here

내가 개별적으로 그 경로를 생성 (아래 코드 참조)하지만 난 CABasicAnimation로 애니메이션을 할 때 내가 대신 처음에 십자가의이 얻을 :

enter image description here

그리고 나서 이것은 체크 표시로 바뀝니다. 왜? 어떻게 해결할 수 있습니까? 아니면 CABasicAnimation과 함께 작동하지 않습니까?

// just the blue background 
    let shapeLayer = CAShapeLayer() 
    shapeLayer.frame = CGRect(x: 0, y: 0, width: bounds.height, height: bounds.height) 
    shapeLayer.backgroundColor = UIColor.blue.cgColor 
    layer.addSublayer(shapeLayer) 


    // first path (the "x") 
    let padding: CGFloat = 10 
    let crossPath = UIBezierPath() 
    crossPath.move(to: CGPoint(x: padding, y: padding)) 
    crossPath.addLine(to: CGPoint(x: bounds.height - padding, y: bounds.height - padding)) 
    crossPath.move(to: CGPoint(x: bounds.height - padding, y: padding)) 
    crossPath.addLine(to: CGPoint(x: padding, y: bounds.height - padding)) 

    // Create the shape layer that will draw the path 
    let signLayer = CAShapeLayer() 
    signLayer.frame = bounds 
    signLayer.path = crossPath.cgPath // setting the path from above 
    signLayer.strokeColor = UIColor.white.cgColor 
    signLayer.fillColor = UIColor.clear.cgColor 
    signLayer.lineWidth = 4 

    // add it to your view's layer and you're done! 
    layer.addSublayer(signLayer) 

    let anim: CABasicAnimation = CABasicAnimation(keyPath: "path") 
    anim.duration = 10.0 // just a test value to see the animation more clearly 

    // the checkmark 
    let checkmarkPath = UIBezierPath() 
    checkmarkPath.move(to: CGPoint(x: bounds.height - padding, y: padding)) 
    checkmarkPath.addLine(to: CGPoint(x: 2 * padding, y: bounds.height - padding)) 
    checkmarkPath.addLine(to: CGPoint(x: padding, y: bounds.height/2)) 

    anim.fromValue = crossPath.cgPath 
    anim.toValue = checkmarkPath.cgPath 

    signLayer.path = checkmarkPath.cgPath 
    signLayer.add(anim, forKey: "path") 

답변

2

올바르게 애니메이션을 적용하려면 동일한 수의 점으로 두 경로 사이를 움직여야합니다. 귀하의 crossPath는 4 점이며 checkmarkPath는 3입니다.

+1

누가 알았습니까? 모양 레이어에서 '경로'애니메이션의 일반적인 규칙입니까? – matt

+0

예. 그것은 경로들 사이를 보간하려고 시도하고 있으며, 움직이는 점들에 의해 그렇게합니다. 따라서 한 모양의 점 A는 다른 모양의 점 A에 해당합니다. 번호가 정렬되지 않으면 원하는 것을 알아 내려고 시도하면서 모든 종류의 이상한 행동을 취하게됩니다. –

+0

원과 사각형 사이를 보간 할 수있는 이유가 궁금합니다. – matt

관련 문제