【发布时间】:2023-03-25 19:56:01
【问题描述】:
我正在尝试使用 CABasicAnimation 键路径但是为图层路径属性设置动画。没有得到准确的结果
我正在寻找的路径动画变化如下
预期的动画
注意:-请忽略开关.. 我的重点只是路径从circular 更改为moon 动画
我尝试了什么
这是我尝试获取此路径转换动画的代码
@IBDesignable class CustomView: UIView {
private lazy var shapeLayer = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
shapeLayer.frame = bounds
shapeLayer.fillColor = UIColor.black.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 3
layer.addSublayer(shapeLayer)
}
override func layoutSubviews() {
shapeLayer.path = drawSunShape(bounds)
}
private func drawSunShape(_ group:CGRect) -> CGPath {
let bezierPath = UIBezierPath()
let radius = group.maxX/2
let center = CGPoint(x: group.midX, y: group.midY)
bezierPath.addArc(withCenter:center, radius: radius, startAngle: -.pi/2, endAngle: 3 * .pi * 0.5 , clockwise: true)
return bezierPath.cgPath
}
private func drawMoonShape(_ group:CGRect) -> CGPath {
let rad : CGFloat = group.maxX/2
let center = CGPoint(x: group.midX, y: group.midY)
let big = UIBezierPath()
big.addArc(withCenter: center, radius: rad, startAngle:-.pi/2.0, endAngle: .pi/2.0, clockwise: true)
big.addArc(withCenter: CGPoint(x: rad * cos(.pi/2.0), y: center.y), radius: rad * sin(.pi/2.0), startAngle: .pi/2.0, endAngle: -.pi/2.0, clockwise: false)
big.close()
return big.cgPath
}
private func animateToOffShape() {
let anim = CABasicAnimation(keyPath: "path")
anim.toValue = drawMoonShape(bounds)
anim.duration = 5.0
shapeLayer.add(anim, forKey: nil)
}
}
我从这段代码中得到了什么
更新
我能够在@sweeper 和@max 的帮助下实现这一目标
但我想要它从中心右侧
我如何达到预期结果的任何指针。谢谢
【问题讨论】:
-
动画似乎工作正常。你只需要画一个更好的月亮形状。
-
第一个视图中的动画是从左侧开始的......但我创建的那个是从顶部开始的......请指导我如何画出更好的月亮请任何指点?
-
为什么不使用示例 gif 中的掩码。舍入蒙版在另一个舍入期间扩大它的大小和位置,这将比实际的转换 4 个锚点对象更平滑,这会给你一个像你的例子一样的 90 度角
-
@jayr 我想用路径动画先生来实现它...
-
实际上我正在创建一个开关库.. 我在其中给出了开关路径和关闭开关路径以进行动画处理
标签: ios swift uibezierpath cashapelayer cabasicanimation