【问题标题】:How to animate object using bezier path?如何使用贝塞尔路径为对象设置动画?
【发布时间】:2019-07-08 09:13:39
【问题描述】:

这是我绘制虚线的代码

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")
}

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

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

任何帮助将不胜感激

【问题讨论】:

    标签: ios swift uibezierpath cgpoint cakeyframeanimation


    【解决方案1】:

    你快到了

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

    这里是工作示例代码

    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/

    【讨论】:

    • 好的,我做得比较好,有帧问题,这就是动画无法按预期工作的原因
    • 我不想要贝塞尔路径的完整动画,我想要蓝点到红点的动画,而不是 360 度
    猜你喜欢
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2014-08-13
    相关资源
    最近更新 更多