Ios 使用核心动画制作UIView动画?
我在使用核心动画正确设置UiView动画时遇到了一些问题。这是我的密码:Ios 使用核心动画制作UIView动画?,ios,animation,uiview,core-animation,Ios,Animation,Uiview,Core Animation,我在使用核心动画正确设置UiView动画时遇到了一些问题。这是我的密码: CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"position"]; [animation setFromValue:[NSValue valueWithCGPoint:self.view.layer.position]]; [animation setToValue:[NSValue valueWithCGPoint:CGPo
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:
方法来实现它,而不是使用多个回调
感谢您的帮助和建议。- 像这样试试
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"];
如果您的目标是移动视图,则代码应如下所示:
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"];
您应该真正更改视图(图层)的位置。否则,在删除动画时,视图将保持在其以前的位置。这是
ui查看动画
和Core动画
之间的区别。对于反弹,您可以尝试在位置上使用关键帧动画。
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"];