CAKeyframeAnimation手动进度 [英] CAKeyframeAnimation Manual Progress
问题描述
我有一个UIView,它的后备层有一个CAKeyframeAnimation,简单的直线路径设置为`path`。
我可以让动画冻结,可以这么说,并手动更改其进度?
例如:
如果路径长度为100个点,则将进度(偏移?)设置为0.45应使视图沿路径向下移动45个点。
I have a UIView whose backing layer has a CAKeyframeAnimation with a simple straight line path set as its `path`.
Can I have the animation "frozen", so to speak, and manually change its progress?
For example:
If the path is 100 points in length, setting the progress (offset?) to 0.45 should have the view move 45 points down the path.
我记得通过CAMediaTiming接口看到一篇文章做了类似的事情(根据滑块上的值移动视图)但是,即使经过几个小时的搜索,我也找不到它。如果我以完全错误的方式接近这个,请告诉我。谢谢。
I remember seeing an article that did something similar (moving a view along a path based on the value from a slider) via CAMediaTiming interfaces, but I haven't been able to find it, even after a few hours of searching. If I'm approaching this in a completely wrong way, please do let me know. Thanks.
以下是一些示例代码,如果上述内容不够清晰。
Here's some sample code, if the above isn't clear enough.
- (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
}
推荐答案
这是基于Jonathan的内容说,只有一点点。动画设置正确,但滑块操作方法应如下所示:
This is based on what Jonathan said, only a bit more to the point. The animation is set up correctly, but the slider action method should be as follows:
- (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
。
This will make the label move along the animation's path
based on timeOffset
.
这篇关于CAKeyframeAnimation手动进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!