gpt4 book ai didi

ios - 使用 CABasicAnimation 正确设置圆半径动画

转载 作者:行者123 更新时间:2023-11-28 21:06:08 25 4
gpt4 key购买 nike

我想使用 CABasicAnimation 扩大和收缩一个圆圈。我以前设计了一种干净有效的方法来使用 sinUIView 类的 drawRect 方法绘制动画,但不幸的是,您似乎可以无论您做什么,都不要在 UIView.animate 方法中设置动画或强制 drawRect 触发。因此,我不得不想出一个使用 CABasicAnimation 的解决方法。结果有点效果,但动画很丑而且看起来不对。这是我的意思的一个例子:

enter image description here

在膨胀和收缩过程中发生翘曲,并且在圆膨胀时右侧出现凹痕或空洞。这是负责动画的类:

struct PathConfig {
var arcCenter: CGPoint
var radius: CGFloat
let startAngle: CGFloat = 0.0
let endAngle: CGFloat = CGFloat(2 * M_PI)
}

class RGSStandbyIndicatorView: UIView {

// MARK: - Variables & Constants

/// The drawing layer.
var shapeLayer: CAShapeLayer!

// MARK: - Class Methods

/// Animation function.
func animate(period: CFTimeInterval) {
let p: PathConfig = PathConfig(arcCenter: CGPoint(x: frame.width / 2, y: frame.height / 2) , radius: 17.5)
let b: UIBezierPath = UIBezierPath(arcCenter: p.arcCenter, radius: p.radius, startAngle: p.startAngle, endAngle: p.endAngle, clockwise: true)
let a: CABasicAnimation = CABasicAnimation(keyPath: "path")

a.fromValue = shapeLayer.path
a.toValue = b.cgPath
a.autoreverses = true
a.duration = period
a.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
a.repeatCount = HUGE
shapeLayer.add(a, forKey: "animateRadius")
}

/// Initializes and returns a CAShapeLayer with the given draw path and fill color.
private class func shapeLayerForPath(_ path: CGPath, with fillColor: CGColor) -> CAShapeLayer {
let shapeLayer: CAShapeLayer = CAShapeLayer()
shapeLayer.path = path
shapeLayer.fillColor = fillColor
return shapeLayer
}

/// Configures the UIView
private func setup() {

// Init config, path.
let cfg: PathConfig = PathConfig(arcCenter: CGPoint(x: frame.width / 2, y: frame.height / 2) , radius: 0.0)
let path: UIBezierPath = UIBezierPath(arcCenter: cfg.arcCenter, radius: cfg.radius, startAngle: cfg.startAngle, endAngle: cfg.endAngle, clockwise: true)

// Init CAShapeLayer
shapeLayer = RGSStandbyIndicatorView.shapeLayerForPath(path.cgPath, with: UIColor.white.cgColor)

layer.addSublayer(shapeLayer)
}

// MARK: Class Method Overrides

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

}

我已经尝试调整开始和结束角度,希望它是没有重叠的路径的一些伪像,但它没有改变任何东西。我不确定如何解决这个问题,它基于 Chadwick Wood 的 answer到一个类似的问题。

最佳答案

使用这个 UIView 子类

class RadioAnimationView: UIView {

var animatableLayer : CAShapeLayer?

override func awakeFromNib() {
super.awakeFromNib()
self.layer.cornerRadius = self.bounds.height/2

self.animatableLayer = CAShapeLayer()
self.animatableLayer?.fillColor = self.backgroundColor?.cgColor
self.animatableLayer?.path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.layer.cornerRadius).cgPath
self.animatableLayer?.frame = self.bounds
self.animatableLayer?.cornerRadius = self.bounds.height/2
self.animatableLayer?.masksToBounds = true
self.layer.addSublayer(self.animatableLayer!)
self.startAnimation()
}


func startAnimation()
{
let layerAnimation = CABasicAnimation(keyPath: "transform.scale")
layerAnimation.fromValue = 1
layerAnimation.toValue = 3
layerAnimation.isAdditive = false
layerAnimation.duration = CFTimeInterval(2)
layerAnimation.fillMode = kCAFillModeForwards
layerAnimation.isRemovedOnCompletion = true
layerAnimation.repeatCount = .infinity
layerAnimation.autoreverses = true

self.animatableLayer?.add(layerAnimation, forKey: "growingAnimation")
}

}

希望对你有帮助

关于ios - 使用 CABasicAnimation 正确设置圆半径动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45601083/

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