gpt4 book ai didi

ios - 如何使用贝塞尔路径为对象设置动画?

转载 作者:行者123 更新时间:2023-11-28 07:24:40 25 4
gpt4 key购买 nike

enter image description here

这是我绘制虚线的代码

func drawLine() {

let shapeLayer = CAShapeLayer()
shapeLayer.bounds = viewDraw.bounds
shapeLayer.position = CGPoint(x: viewDraw.bounds.width/2, y: viewDraw.bounds.height/2)
shapeLayer.fillColor = nil
shapeLayer.strokeColor = ColorConstants.ThemeColor.cgColor
shapeLayer.lineWidth = 3
shapeLayer.lineJoin = CAShapeLayerLineJoin.round // Updated in swift 4.2
shapeLayer.lineDashPattern = [10]

let path = UIBezierPath(roundedRect: viewDraw.bounds, cornerRadius: viewDraw.bounds.size.height/2)
shapeLayer.path = path.cgPath

viewDraw.layer.addSublayer(shapeLayer)

let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.duration = 1
shapeLayer.add(animation, forKey: "MyAnimation")
}

现在,根据我的屏幕截图,我想在虚线无限时间内将蓝点动画化为红点。当蓝点到达红点时,它将以蓝点点重新开始。

注意:整个屏幕是动态的,根据屏幕高度画线也是动态的

任何帮助将不胜感激

最佳答案

你快到了

现在您必须使用具有属性 pathCAKeyframeAnimation 添加动画,您可以在其中传递要设置动画的路径

这里是工作示例代码

func drawLine() {

let shapeLayer = CAShapeLayer()
shapeLayer.bounds = self.view.bounds
shapeLayer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height/2)
shapeLayer.fillColor = nil
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 3
shapeLayer.lineJoin = CAShapeLayerLineJoin.round // Updated in swift 4.2
shapeLayer.lineDashPattern = [10]

let path = UIBezierPath(roundedRect: self.view.bounds, cornerRadius: self.view.bounds.size.height/2)
shapeLayer.path = path.cgPath

self.view.layer.addSublayer(shapeLayer)

let animation1 = CABasicAnimation(keyPath: "strokeEnd")
animation1.fromValue = 0
animation1.duration = 1
shapeLayer.add(animation1, forKey: "MyAnimation")

// Create squareView and add animation
let animation = CAKeyframeAnimation(keyPath: #keyPath(CALayer.position))
animation.duration = 1
animation.repeatCount = MAXFLOAT
animation.path = path.cgPath

let squareView = UIView()

// Don't worry about x,y and width and height it will not affect the animation
squareView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
squareView.backgroundColor = .lightGray
view.addSubview(squareView)
// You can also pass any unique string value for key
squareView.layer.add(animation, forKey: nil)

}

您可以查看本教程以获得更多帮助http://mathewsanders.com/animations-in-swift-part-two/

关于ios - 如何使用贝塞尔路径为对象设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56931719/

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