gpt4 book ai didi

ios - 圆形动画不起作用

转载 作者:行者123 更新时间:2023-11-30 12:30:19 24 4
gpt4 key购买 nike

我正在 View 中绘制一个加载圆。如果我直接使用“初始 View Controller ”打开 View 。圆圈正在动画,但如果我选择从以前的 View (桌面 View )打开它。它带有绘图功能,但没有动画。我试图在viewdidload中调用setup()、didawake函数,但仍然没有机会。它不起作用。

class SpinningView: UIView {

let circleLayer = CAShapeLayer()

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

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

override func awakeFromNib() {
super.awakeFromNib()
setup()
}
@IBInspectable var lineWidth: CGFloat = 4 {
didSet {
circleLayer.lineWidth = lineWidth
setNeedsLayout()
}
}

func setup() {
circleLayer.lineWidth = lineWidth
circleLayer.lineWidth = 10
circleLayer.fillColor = nil
circleLayer.strokeColor = UIColor(red: 0.8078, green: 0.2549, blue: 0.2392, alpha: 1.0).cgColor
layer.addSublayer(circleLayer)
tintColorDidChange()
updateAnimation()
}
func updateAnimation() {
if animating {
circleLayer.add(strokeEndAnimation, forKey: "strokeEnd")
circleLayer.add(strokeStartAnimation, forKey: "strokeStart")
}
else {
circleLayer.removeAnimation(forKey: "strokeEnd")
circleLayer.removeAnimation(forKey: "strokeStart")
}
}
override func layoutSubviews() {
super.layoutSubviews()

let center = CGPoint(x: bounds.midX, y: bounds.midY)
let radius = min(bounds.width, bounds.height) / 2 - circleLayer.lineWidth/2

let startAngle = CGFloat(-M_PI_2)
let endAngle = startAngle + CGFloat(M_PI * 2)
let path = UIBezierPath(arcCenter: CGPoint.zero, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)

circleLayer.position = center
circleLayer.path = path.cgPath
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setup()
}
override func tintColorDidChange() {
super.tintColorDidChange()
circleLayer.strokeColor = tintColor.cgColor
}
@IBInspectable var animating: Bool = true {
didSet {
updateAnimation()
}
}

let strokeEndAnimation: CAAnimation = {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 1
animation.duration = 2
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

let group = CAAnimationGroup()
group.duration = 2.5
group.repeatCount = MAXFLOAT
group.animations = [animation]

return group
}()

let strokeStartAnimation: CAAnimation = {
let animation = CABasicAnimation(keyPath: "strokeStart")
animation.beginTime = 0.5
animation.fromValue = 0
animation.toValue = 1
animation.duration = 2
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

let group = CAAnimationGroup()
group.duration = 2.5
group.repeatCount = MAXFLOAT
group.animations = [animation]

return group
}()


}

最佳答案

我通过在 viewdidappear 中调用 awakefromNib() 函数解决了这个问题。所以我的代码创建了一个像谷歌圆圈一样动画的圆圈。

override func viewDidAppear(_ animated: Bool) {
mSpinningView.awakeFromNib()
}

关于ios - 圆形动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43637530/

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