我有一个UIView,它的背景层有一个CAKeyframeAnimation,其path设置为一条简单的直线路径。
我是否可以让动画“冻结”,并手动更改其进度?
例如:如果路径长度为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
}发布于 2013-07-05 02:16:36
这是基于Jonathan所说的,只是稍微切中要害。动画设置正确,但滑块动作方法应如下所示:
- (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沿动画的path移动。
发布于 2013-07-04 16:38:25
是的,您可以使用CAMediaTiming接口来实现这一点。您可以将layer的speed设置为0,然后手动设置timeOffset。简单的pause/resume方法示例:
- (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;
}
}https://stackoverflow.com/questions/17464974
复制相似问题