【发布时间】:2021-08-09 03:02:52
【问题描述】:
我正在为一个应用程序制作一些自定义类型的加载器,我只需在我的视图中创建 4 个圆形层,所有这些都设置并显示良好。但是当我对所有图层应用脉冲动画缩放时,它们会干扰我的所有设计和图层缩放,但它会改变它的中心点。我希望所有圆形图层首先缩放图层并使其尺寸变小并使其变大然后再次标识并且它应该称为无穷大。但不应该改变它的中心点。
我在这里分享我的代码
class ViewController: BaseViewController{
@IBOutlet weak var layerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
drawCircles()
}
func drawCircles(){
let width = layerView.bounds.width
let halfOfView = width / 2
let firstCirleLayer = UIBezierPath(arcCenter: CGPoint(x: halfOfView / 2, y: halfOfView), radius: halfOfView / 3.5, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
circleLayers(path: firstCirleLayer.cgPath, color: UIColor.purple)
let secondCirleLayer = UIBezierPath(arcCenter: CGPoint(x: halfOfView, y: halfOfView / 2), radius: halfOfView / 3.5, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
circleLayers(path: secondCirleLayer.cgPath, color: UIColor.yellow)
let thirdCirleLayer = UIBezierPath(arcCenter: CGPoint(x: width * 0.75, y: halfOfView), radius: halfOfView / 3.5, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
circleLayers(path: thirdCirleLayer.cgPath, color: UIColor.brown)
let fourthCirleLayer = UIBezierPath(arcCenter: CGPoint(x: halfOfView, y: width * 0.75), radius: halfOfView / 3.5, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
circleLayers(path: fourthCirleLayer.cgPath, color: UIColor.blue)
}
func circleLayers(path: CGPath, color: UIColor){
let layer = CAShapeLayer()
layer.path = path
layer.fillColor = color.cgColor
layerView.layer.addSublayer(layer)
let scaling = CABasicAnimation(keyPath: "transform.scale")
scaling.toValue = 1.2
scaling.duration = 0.3
scaling.autoreverses = true
scaling.repeatCount = .infinity
layer.add(scaling, forKey: nil)
}
}
【问题讨论】:
标签: swift core-animation cashapelayer cabasicanimation ios-animations