使用核心动画让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:
,这种方法非常有效。但是,我试图实现一种微妙的“反弹”效果,而我更倾向于使用setTimingFunction:functionWithControlPoints:
方法实现它而不是多次回调。
感谢任何帮助和建议。
答案 0 :(得分: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"];
答案 1 :(得分: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 Animation
和Core Animation
之间的差异。
答案 2 :(得分: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"];