gpt4 book ai didi

ios - 动画 UIBezierPaths- 不工作- Swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:47 26 4
gpt4 key购买 nike

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

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

2.箭头用黑色填充,而不仅仅是轮廓(我的目标)

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)

如果有人能帮助我解决这些问题,那就太好了。任何帮助将不胜感激!非常感谢。

干杯,西奥

最佳答案

问题是这个顺序:

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

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

此外,您还需要设置形状图层的fillColorstrokeColor

这是实际代码。在一个地方,你会说这样的话:

    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

然后,在稍后的某个时候,你会说:

    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

enter image description here

关于ios - 动画 UIBezierPaths- 不工作- Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43006518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com