2012-06-03 4 views
22

사각형의 UIBezierPath를 삼각형 모양으로 애니메이션하고 싶습니다. 경로의 뷰에 애니메이션을 적용하는 것이 아니라 경로 자체를 애니메이션화하여 한 도형에서 다른 도형으로 변형합니다. 경로는 내가 코어 애니메이션에 대한 전문가 모르지만UIBezierPath 애니메이션하는 방법

CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; 
morph.duration = 5; 
morph.toValue = (id) [self getTrianglePath]; 
[maskLayer addAnimation:morph forKey:nil]; 

첫 번째 줄을 다음과 같이 당신이 CABasicAnimation을 정의 할 수있는 CALayer

CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.fillColor = [[UIColor whiteColor] CGColor];  
maskLayer.path = [self getRectPath]; 

-(CGPathRef)getTrianglePath 
{ 
    UIBezierPath* triangle = [UIBezierPath bezierPath]; 
    [triangle moveToPoint:CGPointZero]; 
    [triangle addLineToPoint:CGPointMake(width(self.view),0)]; 
    [triangle addLineToPoint:CGPointMake(0, height(self.view))]; 
    [triangle closePath]; 
    return [triangle CGPath]; 
} 

-(CGPathRef)getRectPath 
{ 
    UIBezierPath*rect = [UIBezierPath bezierPathWithRect:self.view.frame]; 
    return [rect CGPath]; 
} 

답변

28

에 추가하면 변경 애니메이션을 정의하도록 명시되어있다 path 층의 특성. 두 번째 줄에는 애니메이션이 5 초 걸리고 세 번째 줄은 애니메이션의 최종 상태가 표시됩니다. 마지막으로 애니메이션을 레이어에 추가합니다. 키는 애니메이션의 고유 식별자입니다. 즉, 동일한 키가있는 두 개의 애니메이션을 추가하면 마지막 하나만 고려됩니다. 이 키는 소위 암시 적 애니메이션을 재정의하는 데에도 사용할 수 있습니다. 기본적으로 애니메이션이 적용되는 두 가지 속성 인 CALayer이 있습니다. 좀 더 정확히 말하자면, 이러한 속성 중 하나를 변경하면 변경 사항의 지속 시간이 0.25가됩니다.

+0

감사합니다. 나는 CAKeyframeAnimation을 사용하고 있었다. –

관련 문제