动画UIBezierPaths-无法正常工作-Swift [英] Animate UIBezierPaths- not working- Swift

查看:64
本文介绍了动画UIBezierPaths-无法正常工作-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个动画在一起的箭头.但是,有两个问题:

I am trying to create an arrow that animates together. But, there are two problems:

1.线条没有动画,整个箭头仅出现在屏幕上

1.The lines are not animating, the whole arrow is just appearing on screen

2.箭头填充为黑色,而不仅仅是轮廓(我要针对的是

2.The arrow is filled in black, not just the outlines (which I am aiming for)

let shapeLayer = CAShapeLayer()
let bezierPath = UIBezierPath()

//set up lines
bezierPath.move(to: CGPoint(x: 50.5, y: 158.5))
bezierPath.addLine(to: CGPoint(x: 221.5, y: 158.5))
bezierPath.addLine(to: CGPoint(x: 171.5, y: 108.5))
bezierPath.addLine(to: CGPoint(x: 197.5, y: 82.5))
bezierPath.addLine(to: CGPoint(x: 292.5, y: 177.5))
bezierPath.addLine(to: CGPoint(x: 197.5, y: 272.5))
bezierPath.addLine(to: CGPoint(x: 171.5, y: 246.5))
bezierPath.addLine(to: CGPoint(x: 221.5, y: 196.5))
bezierPath.addLine(to: CGPoint(x: 50.5, y: 196.5))
bezierPath.addLine(to: CGPoint(x: 50.5, y: 158.5))
UIColor.black.setStroke()
bezierPath.lineWidth = 1
bezierPath.stroke()
shapeLayer.path = bezierPath.cgPath

// set up animation
let animation2 = CABasicAnimation(keyPath: "strokeEnd")

animation2.fromValue = 0.0
animation2.toValue = 1.0
animation2.duration = 2.5
shapeLayer.add(animation2, forKey: "drawLineAnimation")

eeView.layer.addSublayer(shapeLayer)

如果有人可以帮助我,那将是惊人的.任何帮助将不胜感激!提前非常感谢.

If anyone could help me with their of those that would be amazing. Any help would be immensely appreciated!! Thanks so much in advance.

干杯,西奥

推荐答案

问题是此顺序:

shapeLayer.add(animation2, forKey: "drawLineAnimation")
eeView.layer.addSublayer(shapeLayer)

您不能将动画添加到图层,然后再将图层添加到界面.您只能在界面中已经的图层中添加动画.

You cannot add an animation to a layer and then add the layer to the interface. You can only add animation to a layer that is already in the interface.

此外,您需要设置形状图层的 fillColor strokeColor .

Also, you need to set the shape layer's fillColor and strokeColor.

这是实际的代码.在一个地方,您会说出这样的话:

Here's actual code. In one place, you would say something like this:

    let shapeLayer = CAShapeLayer()
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.black.cgColor
    eeView.layer.addSublayer(shapeLayer)
    self.shape = shapeLayer // self.shape is a property

然后,在以后的某个时间,您会说:

Then, at some later time, you would say:

    let shapeLayer = self.shape!
    let bezierPath = UIBezierPath()
    // ... rest of your code goes here: create path, add path to shape layer ...
    // ... create animation, add animation to shape layer

这篇关于动画UIBezierPaths-无法正常工作-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆