2013-08-08 4 views
0

핵심 애니메이션을 사용하여 UiView를 적절히 애니메이트하는 데 약간의 문제가 있습니다.코어 애니메이션을 사용하여 애니메이션에 애니메이션을 적용 하시겠습니까?

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
[animation setFromValue:[NSValue valueWithCGPoint:self.view.layer.position]]; 
[animation setToValue:[NSValue valueWithCGPoint:CGPointMake(250, self.view.layer.position.y)]]; //I want to move my UIView 250 pts to the right 
[animation setDuration:1]; 
[animation setDelegate:self]; //where self is the view controller of the view I want to animate 
[animation setRemovedOnCompletion:NO]; 
[self.view.layer addAnimation:animation forKey:@"toggleMenu"]; //where self.view returns the view I want to animate 

나는 또한 다음 대리자 메서드를 구현 한 :

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{ 
    if(flag) 
     [self.view.layer setTransform:CATransform3DMakeTranslation(250, 0, 0)]; //set layer to its final position 
} 

나는 그것이 그렇게 UIView의 오른쪽으로 250 포인트를 이동하는 것을 만들려고 노력하고 있어요 여기 내 코드입니다. 그러나 애니메이션이 트리거되면 내보기가 움직이기 시작하지만 애니메이션이보기가 250 포인트 오른쪽으로 이동하기 전에 애니메이션이 종료 된 것처럼 보이기 때문에 UIView의 '텔레포트'가 최종 위치로 이동합니다. 이 문제의 원인을 파악할 수 없습니다.

나는 또한 UIView 메서드 +(void)animateWithDuration:animations:을 사용하려고 시도했으며이 방법은 완벽하게 작동합니다. 그러나 미묘한 '바운스 (bounce)'효과를 얻으 려 노력하고 있으며 다중 콜백을 사용하는 대신 setTimingFunction:functionWithControlPoints: 메서드를 사용하여 훨씬 성취 할 수 있습니다.

모든 도움과 제안을 부탁드립니다.

+0

바운스의 경우 키 프레임 애니메이션을 사용할 수 있습니다. – nielsbot

답변

1

CABasicAnimation * rotationAnimation 같은 시도; 당신이 당신의보기를 이동하는 것을 목표로하는 경우

rotationAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; 

    rotationAnimation.fromValue=[NSValue valueWithCGPoint:CGPointMake([view center].x, [view center].y)]; 
    rotationAnimation.toValue=[NSValue valueWithCGPoint:CGPointMake([view center].x+250, [view center].y)]; 

    rotationAnimation.duration = 1.0+i; 
    rotationAnimation.speed = 3.0; 

    rotationAnimation.cumulative = YES; 
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

    [view.layer addAnimation:rotationAnimation forKey:@"position"]; 
+0

'center'속성을 사용하는 것이 트릭을 만든 것처럼 보였습니다. –

0

, 코드는 다음과 같이 가야한다 :

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
[animation setFromValue:[NSValue valueWithCGPoint:self.view.layer.position]]; 
CGPoint p = self.view.layer.position; 
p.x += 250; 
self.view.layer.position = p; 
[animation setDuration:1]; 
[animation setRemovedOnCompletion:NO]; 
[self.view.layer addAnimation:animation forKey:@"toggleMenu"]; 

당신은 정말보기의 (레이어) 위치를 변경해야합니다. 그렇지 않으면 애니메이션을 제거 할 때 뷰가 이전 위치에 유지됩니다. 이는 UIView AnimationCore Animation의 차이입니다.

0
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
[animation setFromValue:[NSValue valueWithCGPoint:view.layer.position]]; 
CGPoint newPosition = CGPointMake(200, 300); 
view.layer.position = p; 
[animation setDuration:0.5f]; 
[animation setRemovedOnCompletion:NO]; 
[group setFillMode:kCAFillModeForwards]; 
[[view layer] addAnimation:animation forKey:@"position"]; 
관련 문제