ios - 沿着路径的点

标签 ios swift calayer caanimation

我正在寻找一些简单的方法来为点按闭合路径移动设置动画。

我有一些使用 UIBezierPath 创建的路径。由 CAShapeLayer 表示。

我想在此形状上放置将围绕此闭合路径移动的点。问题是我找不到最好的方法。我不想以编程方式添加每个点。也许有一些方法可以使用粒子。

路径会有不同的形状。最重要的是请点之间的距离相同。

我很乐意提供帮助

最佳答案

这是您可以在 playground 中运行的示例。

它基于 Matt Long 的一篇文章(不久前)http://www.cimgf.com/2009/10/20/marching-ants-with-core-animation/

import UIKit
import PlaygroundSupport

let container = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 700))

container.backgroundColor = UIColor.green

let v = UIView(frame: CGRect(x: 100, y: 100, width: 300, height: 300))
v.backgroundColor = UIColor.yellow

let shp = CAShapeLayer()
shp.bounds = v.bounds
let pth = UIBezierPath()
pth.move(to: CGPoint(x: 20, y: 220))
pth.addLine(to: CGPoint(x: 20, y: 40))
pth.addLine(to: CGPoint(x: 40, y: 80))
pth.addLine(to: CGPoint(x: 120, y: 40))
pth.addLine(to: CGPoint(x: 140, y: 40))
pth.close()
shp.strokeColor = UIColor.orange.cgColor
shp.fillColor = UIColor.cyan.cgColor
shp.lineWidth = 3.0

shp.lineDashPattern = [5, 5]

shp.path = pth.cgPath
shp.position = CGPoint(x: 150, y: 150)
v.layer.addSublayer(shp)

container.addSubview(v)

let dashAnim = CABasicAnimation(keyPath: "lineDashPhase")
dashAnim.fromValue = 0
dashAnim.toValue = 15
dashAnim.duration = 0.75
dashAnim.repeatCount = 10000

shp.add(dashAnim, forKey: "lineDashPhase")

PlaygroundPage.current.liveView = container
PlaygroundPage.current.needsIndefiniteExecution = true

关于ios - 沿着路径的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43052384/

相关文章:

ios - 如何在预定义的矩形中显示图像的缩放部分?

iphone - iOS 相当于 Android View.GONE 可见性模式

ios - 等到委托(delegate)方法在 ios 中完成执行

objective-c - 跨模型获取的属性

objective-c - 使用 AVVideoCompositionCoreAnimationTool 在 CALayer 中将视频与静态图像混合

ios - 如何随 UIView 一起移动子图层?

ios - 使用超链接集成 UPI 后如何获得响应

ios - 单击按钮时如何使用用户当前位置显示注释?

ios - SwiftUI 如果让内部 View

ios - 在 Swift 中使用私有(private) API 在 WkWebView 中启用应用程序缓存不起作用