【问题标题】:Animate bezier straight lines动画贝塞尔直线
【发布时间】:2015-08-28 04:41:42
【问题描述】:

我想要如何制作像跷跷板这样的贝塞尔直线动画。有人可以建议。动画应该是什么样子的参考链接 (https://www.youtube.com/watch?v=bjCx128BZK8)。

【问题讨论】:

    标签: ios animation line uibezierpath


    【解决方案1】:

    你没有说你是如何渲染这个UIBezierPath的,但如果你把它添加为CAShapeLayer,你可以只应用一个CAKeyframeAnimation

    // create simple path
    
    let path = UIBezierPath()
    path.moveToPoint(CGPoint(x: 50, y: view.bounds.size.height / 2))
    path.addLineToPoint(CGPoint(x: view.bounds.size.width - 50, y: view.bounds.size.height / 2))
    
    // create `CAShapeLayer` from that path
    
    let layer = CAShapeLayer()
    layer.path = path.CGPath
    layer.lineWidth = 10
    layer.strokeColor = UIColor.blueColor().CGColor
    layer.fillColor = UIColor.clearColor().CGColor
    
    view.layer.addSublayer(layer)
    
    // animate it
    
    let animation = CAKeyframeAnimation(keyPath: "transform")
    var values = [NSValue]()
    for var x: CGFloat = 0.0; x < CGFloat(M_PI * 4); x += CGFloat(M_PI * 4 / 100.0) {
        var transform = CATransform3DMakeTranslation(view.frame.size.width/2, view.frame.size.height/2, 0.0)
        transform = CATransform3DRotate(transform, sin(x) / 3.0, 0.0, 0.0, 1.0)
        transform = CATransform3DTranslate(transform, -view.frame.size.width/2, -view.frame.size.height/2, 0.0)
    
        values.append(NSValue(CATransform3D: transform))
    }
    animation.duration = 2
    animation.values = values
    
    layer.addAnimation(animation, forKey: "transform")
    

    产生:

    或者您可以将CAShapeLayer 添加到视图中,然后使用基于块的UIView 动画来旋转视图。基本思想是一样的。

    如果你想做一些更复杂的事情,你可以使用CADisplayLink,或者更新pathCAShapeLayer(如here所述)或者更新UIView子类使用的属性,然后调用setNeedsDisplay.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      相关资源
      最近更新 更多