Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置CAShapeLayer/CAGradientLayer的动画_Ios_Swift_Jquery Animate_Cagradientlayer - Fatal编程技术网

Ios 设置CAShapeLayer/CAGradientLayer的动画

Ios 设置CAShapeLayer/CAGradientLayer的动画,ios,swift,jquery-animate,cagradientlayer,Ios,Swift,Jquery Animate,Cagradientlayer,这是我昨天问的一个问题的补充。结果很好,但我想修改一下 下面的代码,基于上面的线程,生成了一个CABasicAnimation,它生长出深蓝色的“三角形”。我希望这是一个具有3个停止点的CAGradientLayer,而不是深蓝色的纯色 我尝试添加一个CAGradientLayer来配置渐变,然后使用addSubLayer将渐变添加到制作动画的CAShapeLayer。我在下面发布的结果没有动画效果。颜色和形状保持不变。渐变看起来很棒,只需设置动画即可: 我需要foregroundGradie

这是我昨天问的一个问题的补充。结果很好,但我想修改一下

下面的代码,基于上面的线程,生成了一个CABasicAnimation,它生长出深蓝色的“三角形”。我希望这是一个具有3个停止点的CAGradientLayer,而不是深蓝色的纯色

我尝试添加一个CAGradientLayer来配置渐变,然后使用addSubLayer将渐变添加到制作动画的CAShapeLayer。我在下面发布的结果没有动画效果。颜色和形状保持不变。渐变看起来很棒,只需设置动画即可:

我需要foregroundGradient对象(带有gradientLayer子层!)从左到右设置动画。


如果您从上面的代码中删除渐变,并将foregroundGradient保持为纯色,您将看到动画。

我认为您必须在视图中使用
遮罩,如下所述

//Remove below line
view.layer.addSublayer(backgroundGray)

//Add your mask to view
view.layer.mask = backgroundGray
整个函数如下所示:

func getGradientView() {
    let view = UIView(frame: CGRect(x: 0, y: 50, width: 521, height: 40))
    view.backgroundColor = UIColor.grayColor()
    self.view.addSubview(view)

    let maskPath = UIBezierPath()

    maskPath.moveToPoint(CGPoint(x: 0, y: 30))
    maskPath.addLineToPoint(CGPoint(x: 0, y: 25))
    maskPath.addLineToPoint(CGPoint(x: 300, y: 10))
    maskPath.addLineToPoint(CGPoint(x: 300, y: 30))
    maskPath.closePath()

    let maskLayer = CAShapeLayer()
    maskLayer.path = maskPath.CGPath
    maskLayer.fillColor = UIColor.whiteColor().CGColor

    let rectToAnimateFrom = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 0, height: 40))
    let rectToAnimateTo = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 300, height: 40))

    let backgroundGray = CAShapeLayer()
    backgroundGray.path = maskPath.CGPath
    backgroundGray.fillColor = UIColor.grayColor().CGColor

    let foregroundGradient = CAShapeLayer()
    foregroundGradient.mask = maskLayer
    foregroundGradient.backgroundColor = UIColor.clearColor().CGColor


    let gradientLayer = CAGradientLayer()
    gradientLayer.startPoint = CGPointMake(0, 0)
    gradientLayer.endPoint = CGPointMake(1, 0)
    gradientLayer.frame = maskPath.bounds

    let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor, UIColor.blueColor().CGColor]
    gradientLayer.colors = colors

    foregroundGradient.addSublayer(gradientLayer)

    //Remove below line
    //view.layer.addSublayer(backgroundGray)

    view.layer.addSublayer(foregroundGradient)

    //Add you mask to view
    view.layer.mask = backgroundGray

    let animation = CABasicAnimation(keyPath: "path")
    animation.fromValue = rectToAnimateFrom.CGPath
    animation.toValue = rectToAnimateTo.CGPath
    animation.duration = 1
    animation.repeatCount = 1000
    animation.removedOnCompletion = false
    animation.fillMode = kCAFillModeForwards


    maskLayer.addAnimation(animation, forKey: "fill animation")
}

让我知道它是否符合您的例外情况。

您能告诉我您在这里面临什么问题吗?@Dhanesh-第3段提到了这一点。渐变看起来很完美。我需要的确切尺寸和颜色。但是,动画不起作用。看看我贴的帖子。蓝色从左向右设置动画,而灰色三角形为背景。我需要同样的结果,除了使用梯度。
func getGradientView() {
    let view = UIView(frame: CGRect(x: 0, y: 50, width: 521, height: 40))
    view.backgroundColor = UIColor.grayColor()
    self.view.addSubview(view)

    let maskPath = UIBezierPath()

    maskPath.moveToPoint(CGPoint(x: 0, y: 30))
    maskPath.addLineToPoint(CGPoint(x: 0, y: 25))
    maskPath.addLineToPoint(CGPoint(x: 300, y: 10))
    maskPath.addLineToPoint(CGPoint(x: 300, y: 30))
    maskPath.closePath()

    let maskLayer = CAShapeLayer()
    maskLayer.path = maskPath.CGPath
    maskLayer.fillColor = UIColor.whiteColor().CGColor

    let rectToAnimateFrom = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 0, height: 40))
    let rectToAnimateTo = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 300, height: 40))

    let backgroundGray = CAShapeLayer()
    backgroundGray.path = maskPath.CGPath
    backgroundGray.fillColor = UIColor.grayColor().CGColor

    let foregroundGradient = CAShapeLayer()
    foregroundGradient.mask = maskLayer
    foregroundGradient.backgroundColor = UIColor.clearColor().CGColor


    let gradientLayer = CAGradientLayer()
    gradientLayer.startPoint = CGPointMake(0, 0)
    gradientLayer.endPoint = CGPointMake(1, 0)
    gradientLayer.frame = maskPath.bounds

    let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor, UIColor.blueColor().CGColor]
    gradientLayer.colors = colors

    foregroundGradient.addSublayer(gradientLayer)

    //Remove below line
    //view.layer.addSublayer(backgroundGray)

    view.layer.addSublayer(foregroundGradient)

    //Add you mask to view
    view.layer.mask = backgroundGray

    let animation = CABasicAnimation(keyPath: "path")
    animation.fromValue = rectToAnimateFrom.CGPath
    animation.toValue = rectToAnimateTo.CGPath
    animation.duration = 1
    animation.repeatCount = 1000
    animation.removedOnCompletion = false
    animation.fillMode = kCAFillModeForwards


    maskLayer.addAnimation(animation, forKey: "fill animation")
}