我在这里,听起来像是一个基本问题……
我制作了一个运行CABasicAnimation的简单应用程序:如果我关闭该应用程序并重新打开,则动画仍在继续(这是完美的),但是如果我在同一应用程序中更改屏幕然后又回来,则动画停止(这很糟糕)。
任何提示ti都可以帮助我解决问题并在用户位于同一应用程序的另一个屏幕上时使动画继续运行吗?
override func viewDidLoad() {
super.viewDidLoad()
swipeVC()
myCircle()
}
// Animated circle
func myCircle() {
// Center the shape
let center = view.center
// Create back shape
let backlayer = CAShapeLayer()
let circulationPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
// Designing the animated circle
backlayer.path = circulationPath.cgPath
backlayer.fillColor = UIColor.clear.cgColor
backlayer.strokeColor = UIColor.red.cgColor
backlayer.lineWidth = 5
view.layer.addSublayer(backlayer)
// Designing the animated circle
shapeLayer.path = circulationPath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.lineWidth = 10
shapeLayer.strokeEnd = 0
shapeLayer.lineCap = .round
// Add gesture recgnition to activate the circle animation
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
// Adding the circle to the main view
view.layer.addSublayer(shapeLayer)
}
答案 0 :(得分:0)
据我所知,当应用程序进入后台/导航到其他页面时,IOS会删除动画,这就是为什么您看到“动画已停止”的原因。
我建议您在离开应用程序或页面时保存动画的当前状态(例如完成率),并在返回时恢复动画,例如在viewWillAppear中或添加一个监听UIApplicationWillEnterForeground的观察者
override func viewDidLoad() {
super.viewDidLoad()
...
// listening to when the app will enter foreground
NotificationCenter.default.addObserver(self, selector: #selector(resumeAnimation), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}
override func viewWillAppear() {
super.viewWillAppear()
...
// when come back from other screen
resumeAnimation()
}
private resumeAnimation() {
// re-draw the animation based on stored state
}