Ios CAKeyframeAnimation手动进度
我有一个UIView,它的背景层有一个CAKeyframeAnimation,简单的直线路径被设置为它的“路径”。Ios CAKeyframeAnimation手动进度,ios,core-animation,cakeyframeanimation,Ios,Core Animation,Cakeyframeanimation,我有一个UIView,它的背景层有一个CAKeyframeAnimation,简单的直线路径被设置为它的“路径”。 可以说,我可以将动画“冻结”并手动更改其进度吗? 例如: 如果路径长度为100点,则将“进度”(偏移?)设置为0.45时,视图应沿路径向下移动45点。 我记得看到过一篇文章,它通过CAMediaTiming接口做了类似的事情(根据滑块的值沿路径移动视图),但我没能找到它,甚至在搜索了几个小时之后。如果我以一种完全错误的方式处理这个问题,请一定要让我知道。谢谢 如果上面的内容不
可以说,我可以将动画“冻结”并手动更改其进度吗?
例如: 如果路径长度为100点,则将“进度”(偏移?)设置为0.45时,视图应沿路径向下移动45点。 我记得看到过一篇文章,它通过CAMediaTiming接口做了类似的事情(根据滑块的值沿路径移动视图),但我没能找到它,甚至在搜索了几个小时之后。如果我以一种完全错误的方式处理这个问题,请一定要让我知道。谢谢 如果上面的内容不够清楚,下面是一些示例代码
- (void)setupAnimation
{
CAKeyFrameAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:_label.layer.position];
[path addLineToPoint:(CGPoint){200, 200}];
animation.path = path.CGPath;
animation.duration = 1;
animation.autoreverses = NO;
animation.removedOnCompletion = NO;
animation.speed = 0;
// _label is just a UILabel in a storyboard
[_label.layer addAnimation:animation forKey:@"LabelPathAnimation"];
}
- (void)sliderDidSlide:(UISlider *)slider
{
// move _label along _animation.path for a distance that corresponds to slider.value
}
是的,您可以通过CAMediaTiming界面执行此操作。您可以将
层的速度设置为0
,并手动设置时间偏移量。简单的暂停/恢复方法示例:
- (void)pauseAnimation {
CFTimeInterval pausedTime = [yourLayer convertTime:CACurrentMediaTime() fromLayer:nil];
yourLayer.speed = 0.0;
yourLayer.timeOffset = pausedTime;
}
- (void)resumeAnimation {
CFTimeInterval pausedTime = [yourLaye timeOffset];
if (pausedTime != 0) {
yourLayer.speed = 1.0;
yourLayer.timeOffset = 0.0;
yourLayer.beginTime = 0.0;
CFTimeInterval timeSincePause = [yourLayer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
yourLayer.beginTime = timeSincePause;
}
}
这是基于乔纳森所说的,只是稍微切中要害一点。动画设置正确,但滑块动作方法应如下所示:
- (void)sliderDidSlide:(UISlider *)slider
{
// Create and configure a new CAKeyframeAnimation instance
CAKeyframeAnimation *animation = ...;
animation.duration = 1.0;
animation.speed = 0;
animation.removedOnCompletion = NO;
animation.timeOffset = slider.value;
// Replace the current animation with a new one having the desired timeOffset
[_label.layer addAnimation:animation forKey:@"LabelPathAnimation"];
}
这将使标签基于timeOffset
沿着动画的路径移动。我想你的意思是你的动画。timeOffset
等等。CACurrentMediaTime
接口由层(CALayer)和动画(CAAnimation)实现。如果您使用该层,当前添加到该层的所有动画都会受到影响,如果您使用该动画,则只会影响特定的动画。对,抱歉。我只是刚刚意识到这一点,并且正在编辑我的评论。我不是在寻找暂停/恢复方法(如苹果所示)。我还不清楚该怎么做我在问题中提出的问题。你可以将动画的速度设置为0
(因此根本没有自动移动),然后手动将时间偏移设置为0
(0点)和1
(100点)之间的值。您可以尝试使用UISLider作为时间偏移值的输入。当滑块值达到1.0时,动画将变为初始值。如何处理?@MatterGoal附加动画后,需要设置实际的最终值。动画并没有改变实际值,它只是对该值的变化进行视觉表示。