gpt4 book ai didi

ios - 为 UIBezierPath 创建旋转动画

转载 作者:行者123 更新时间:2023-11-28 05:41:52 26 4
gpt4 key购买 nike

<分区>

我有一个 CAShapeLayer,它是一个以我的 View Controller 的主视图为中心的圆。

看起来像这样:

CAShapeLayer (Circle)

我遇到的问题是,如果我尝试使用以下代码设置旋转动画,它不会围绕圆心旋转,而是在其他地方旋转。

import UIKit

extension UIView {
func addCircle(centerOfCircle: CGPoint, radius: CGFloat, startAngle: CGFloat?, endAngle: CGFloat?) {
let circlePath = UIBezierPath(arcCenter: centerOfCircle, radius: radius, startAngle: startAngle ?? 0, endAngle: endAngle ?? CGFloat(Double.pi * 2), clockwise: true)

let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath

//change the fill color
shapeLayer.fillColor = UIColor.clear.cgColor
//you can change the stroke color
shapeLayer.strokeColor = UIColor.red.cgColor

shapeLayer.lineDashPattern = [1, 10]
shapeLayer.lineCap = .round
//you can change the line width
shapeLayer.lineWidth = 3.0

shapeLayer.anchorPoint = centerOfCircle

let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.toValue = NSNumber(value: Double.pi * 2)
rotation.duration = 2
rotation.isCumulative = true
rotation.repeatCount = Float.greatestFiniteMagnitude



shapeLayer.add(rotation, forKey: "rotationAnimation")

self.layer.addSublayer(shapeLayer)
}
}

然后我得到的是以下内容:

This is what it looks like

非常感谢任何帮助!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com