I have implemented a LoadingLayer class that has some animating elements (2 to be exact) which are animated with CABasicAnimation
. Now, this loading screen view persists throughout the whole application. Does hiding the view stop all animations ? Or let me rephrase that: Is there a lot of memory I have to be worried about or could that potentially cause lags ? I tried to use the -pauseLayer:
and -resumeLayer:
methods but they caused some problems due to multithreading in my application. Is it therefore ok just to hide and show the loading screen with its animations running all the time ?
https://developer.apple.com/library/content/qa/qa1673/_index.html
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
Here is the swift 3 version of Nico's answer.
Hope helps!
fileprivate func pauseLayer(layer: CALayer) {
let pausedTime = layer.convertTime(CACurrentMediaTime(), from: nil)
layer.speed = 0.0
layer.timeOffset = pausedTime
}
fileprivate func resumeLayer(layer: CALayer) {
let pausedTime = layer.timeOffset
layer.speed = 1.0
layer.timeOffset = 0.0
layer.beginTime = 0.0
let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
layer.beginTime = timeSincePause
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With