【问题标题】:gradient under UIBezierPath animationUIBezierPath 动画下的渐变
【发布时间】:2015-09-16 13:06:40
【问题描述】:

我需要有关 iOS 核心动画的帮助

我有以下看法

我需要从左到右为曲线外观设置动画。这一步是由animationWithKeyPath:@"strokeEnd" 完成的,它工作正常,一切都很好。

我还需要对曲线下的渐变外观进行动画处理,随着曲线外观从左到右

这是我需要达到的外观的参考视频剪辑

我怎样才能做到这一点?

我拥有的图层和路径:

(1)渐变曲线-uishapelayer 彩色渐变 - 具有 (1) 作为掩码的渐变层

(2) 闭合路径层 - 沿着 (1) 曲线的路径,但带有 [path closePath]; 灰度梯度 - 以 (2) 作为掩码的梯度

(3) animationLayer - 层,添加为 [self.layer addSublayer:animationLayer] (3) 有彩色渐变和灰度渐变作为子层

initWithFrame:

    _animationLayer = [CALayer new];
    _animationLayer.frame = frame;
    _animationLayer.masksToBounds = YES;
    [self.layer addSublayer:_animationLayer];

    _path = [self characteristicsGraph:frame];

    _pathLayer = [CAShapeLayer new];
    _pathLayer.frame = frame;
    _pathLayer.path = _path.CGPath;
    _pathLayer.strokeColor = [color CGColor];
    _pathLayer.fillColor = nil;
    _pathLayer.lineWidth = lineW;


    _fillPathLayer = [CAShapeLayer new];
    _fillPathLayer.frame = frame;
    _fillPathLayer.path = [self closePath:_path].CGPath;
    _fillPathLayer.strokeColor = [color CGColor];
    _fillPathLayer.fillColor = [color CGColor];
    _fillPathLayer.masksToBounds = YES;


    _fillGradient = [self gradient:frame];
    _fillGradient.frame = frame;
    _fillGradient.mask = _fillPathLayer;

    _lineGradient = [self lineGradient:frame];
    _lineGradient.frame = frame;
    _lineGradient.mask = _pathLayer;

    [_animationLayer addSublayer:_fillGradient];
    [_animationLayer addSublayer:_lineGradient];

pathLayer 动画(动画彩色渐变外观):

self.currentAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
self.currentAnimation.fromValue = @(0);
self.currentAnimation.toValue = @(1);
self.currentAnimation.duration = .5;
[self.pathLayer addAnimation:self.currentAnimation forKey:animationKey];

灰色渐变是静态的,不能设置动画。 将不胜感激帮助

【问题讨论】:

    标签: ios objective-c animation core-graphics core-animation


    【解决方案1】:

    您可以为渐变图层制作一个遮罩层 - 只需将其设为一个矩形,该矩形会随着线条的动画而扩展。

    如果你想 100% 忠实于视频,我注意到的另一个问题是你的渐变需要沿着图表的趋势倾斜 - 注意他们的图表趋势向上,并且渐变角度匹配。

    【讨论】:

    • 是的,最终的解决方案是使用bounds.size.width 属性使父层——包含所有必需层的层——动画化。渐变层是具有 3 种颜色和 3 个控制点的层,被曲线路径层掩盖。是的,谢谢你的建议。它帮助了我。还有另一个问题——“画”线的大圆圈。暂时无法对其进行动画处理,它的图层动画与其余动画不同步。但这是一个不同的问题。
    • 显而易见的问题 - 但圆形动画代码的持续时间和缓动曲线是否匹配?它是否具有匹配的开始值和结束值?我最近刚刚做了一个这样的项目,其中一个点正在为一条线设置动画,并且它确实需要精确度。
    • 我用相同的值在一个地方启动两个动画。一个动画 - bounds.size.width CABasicAnimation 用于容器层,另一个 - position CAKeyFrame 动画。持续时间相同,值不同但构成一个整数。位置动画是一种拖尾宽度动画。
    • 尝试将两个关键帧动画的calculationMode 设置为kCAAnimationPaced - 这将设置中间关键帧的时间以保持整个过程的稳定步伐 - 可能有助于解决对齐问题。 developer.apple.com/library/prerelease/ios/documentation/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多