ios - UIBezierPath数组动画绘制

标签 ios swift swift3 core-animation uibezierpath

我有从 touchesBegan 和 touchesMoved 点创建的 UIBezierPath 数组。 我想在屏幕上绘制动画。

如果不迭代每个贝塞尔曲线并调用 setNeedsDisplay(),最好的方法是什么?

谢谢

  override func draw(_ rect: CGRect) {
      for bezier in beziers{
         bezier.stroke()
      }
   }
   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      super.touchesBegan(touches, with: event)
      let cgPoint = touches.first!.location(in: self)
      let bezierPath = ColorBezierPath() // subclass of UIBezierPath
      bezierPath.lineWidth = lineWidth
      bezierPath.color = color
      bezierPath.move(to: cgPoint)
      beziers.append(bezierPath)
   }

   override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
      super.touchesMoved(touches, with: event)
      let cgPoint = touches.first!.location(in: self)
      let bezierPath = beziers.last
      bezierPath?.addLine(to: cgPoint)
      setNeedsDisplay()
   }

最佳答案

你最好的选择是使用 CAShapeLayer。它需要一条路径和各种其他设置。要为其设置动画,只需将其 strokeEnd 属性从 0.0 设置为 1.0。给定您想要的任何动画时间参数,这将创建从头到尾绘制路径的效果:

let shapeLayer = CAShapeLayer()               // strokeEnd's model value is 1.0 by default
shapeLayer.frame = containerLayer.bounds
shapeLayer.path = bezierPath.cgPath
containerLayer.addSublayer(shapeLayer)

let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd")
strokeEndAnimation.duration = 2.0
strokeEndAnimation.fromValue = 0.0

shapeLayer.add(strokeEndAnimation, forKey: "strokeEndAnimation")

关于ios - UIBezierPath数组动画绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930104/

相关文章:

ios - 为什么我的本地通知没有在 iOS 10 的前台触发?

ios - 自定义 UIView 标签和动画未显示在 View Controller 中

swift - 嵌套的 UICollectionView 单元格内的自定义 UIbutton 不会被触发

ios - 如何在早上 8 点发出每日通知? ( swift 3)

javascript - 为什么 Safari 页面会破坏 iOS 渲染?

iphone - 我可以在 XCode 中保留一个项目,但为 Mac OS X、Apple Mac Store 和 iOS 设备构建它吗?

ios - 如何将搜索栏添加到导航栏

ios - 'pathExtension' 不可用 : Use pathExtension on URL instead

android - (Uber RIB) 跨 RIB 传递数据

IOS 将 2 个 GMSMarkers 之间的箭头线绘制到 Google map 中