【问题标题】:Dots following the path跟随路径的点
【发布时间】:2017-03-27 17:01:28
【问题描述】:

我正在寻找一些简单的方法,可以让点按闭合路径移动。

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

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

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

我会很高兴得到帮助

【问题讨论】:

  • 你试过CAKeypathAnimation吗?
  • 你试过什么? CAShapeLayer 有一个 .lineDashPattern 属性。如果您将其设置为 myShapeLayer.lineDashPattern = [1 3] 或类似名称会怎样?

标签: ios swift calayer caanimation


【解决方案1】:

这是一个可以在操场上运行的示例。

它基于 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

【讨论】:

    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 2015-02-26
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多