首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAKeyframeAnimation手动进度

CAKeyframeAnimation手动进度
EN

Stack Overflow用户
提问于 2013-07-04 16:05:10
回答 2查看 2.6K关注 0票数 5

我有一个UIView,它的背景层有一个CAKeyframeAnimation,其path设置为一条简单的直线路径。

我是否可以让动画“冻结”,并手动更改其进度?

例如:如果路径长度为100点,则设置进度(偏移量?)设置为0.45时,视图将沿路径移动45个点。

我记得看过一篇文章,它通过CAMediaTiming界面做了类似的事情(根据滑块的值沿路径移动视图),但即使经过几个小时的搜索,我也找不到它。如果我以一种完全错误的方式来处理这个问题,请一定要让我知道。谢谢。

如果上面的内容不够清楚,这里有一些示例代码。

代码语言:javascript
复制
- (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
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-05 02:16:36

这是基于Jonathan所说的,只是稍微切中要害。动画设置正确,但滑块动作方法应如下所示:

代码语言:javascript
复制
- (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移动。

票数 3
EN

Stack Overflow用户

发布于 2013-07-04 16:38:25

是的,您可以使用CAMediaTiming接口来实现这一点。您可以将layerspeed设置为0,然后手动设置timeOffset。简单的pause/resume方法示例:

代码语言:javascript
复制
- (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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17464974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档