gpt4 book ai didi

iphone - CAAnimation 在滚动出界时停止

转载 作者:行者123 更新时间:2023-11-28 20:14:07 27 4
gpt4 key购买 nike

我有一个无限旋转的 UIImageView 形式的自定义事件指示器。这个惊人的事件指示器被放置在一个表格单元格中。

动画是这样实现的:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
animation.duration = 1.0f;
animation.repeatCount = INFINITY;

[self.layer addAnimation:animation forKey:@"SpinAnimation"];



起初,它工作得很好......但是当单元格滚动到表格的边界时,动画停止,我不知道如何重置它....../:

请帮忙...
非常感谢...:)

最佳答案

原因是当prepareForReuse 起作用您的应用程序进入后台模式时,rotation.z 键的动画变为零。

考虑手动绘制事件指示器或尝试类似 https://github.com/airbnb/lottie-ios 的方法

下面的方法事件指示器在滚动/单元格/背景模式下运行良好

我尝试了 rotation.z 和一些基于 CAReplicationLayer 的变通方法——在滚动/单元格/背景模式下都失败了

progressLayer look

private func drawLoaderCircle() {

let path = UIBezierPath(arcCenter: loaderViewCenter, radius: loaderCircleRadius, startAngle: 0, endAngle: 2 * .pi, clockwise: true).cgPath

setupGrapes(endAngle: 310, path: path)
}

private func setupGrapes(endAngle: CGFloat, path: CGPath) {

let step: CGFloat = 2
var grapeAnimationDelay: CFTimeInterval = 0

for i in stride(from: 0, to: endAngle, by: step) {

let radians = i * .pi / 180

let x = loaderViewCenter.x + loaderCircleRadius * cos(radians)
let y = loaderViewCenter.y + loaderCircleRadius * sin(radians)

let grapeLayer = CAShapeLayer()
grapeLayer.frame = CGRect(x: 0, y: 0, width: circleWidth + 0.2, height: circleWidth + 0.2)
grapeLayer.position = CGPoint(x: x, y: y)
grapeLayer.cornerRadius = grapeLayer.frame.width / 2
grapeLayer.backgroundColor = loaderColor.cgColor

grapeAnimationDelay += Double(rotateDuration * Double(step) / 360)
let rotateGrapeAnimation = CAKeyframeAnimation(keyPath: "position")
rotateGrapeAnimation.path = path
rotateGrapeAnimation.beginTime = grapeAnimationDelay
rotateGrapeAnimation.calculationMode = .linear
rotateGrapeAnimation.repeatCount = .infinity
rotateGrapeAnimation.isRemovedOnCompletion = false
rotateGrapeAnimation.duration = rotateDuration

grapeLayer.add(rotateGrapeAnimation, forKey: nil)

progressLayer.addSublayer(grapeLayer)
}

}

关于iphone - CAAnimation 在滚动出界时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18817558/

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