From aa6e103e4203044071da0f9a84e33f353f5f11da Mon Sep 17 00:00:00 2001 From: Matthew Seiler Date: Thu, 31 Mar 2016 11:12:49 -0400 Subject: [PATCH] Conditionally invalidate displayLink Don't invalidate the CADisplayLink on deinit unless the lazy display link instance was already initialized previously. - Closes #53 - Closes #54 --- Source/AnimatableImageView.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Source/AnimatableImageView.swift b/Source/AnimatableImageView.swift index 81f36bc..5db3ee3 100644 --- a/Source/AnimatableImageView.swift +++ b/Source/AnimatableImageView.swift @@ -19,8 +19,12 @@ public class AnimatableImageView: UIImageView { /// An `Animator` instance that holds the frames of a specific image in memory. var animator: Animator? + /// A flag to avoid invalidating the displayLink on deinit if it was never created + private var displayLinkInitialized: Bool = false + /// A display link that keeps calling the `updateFrame` method on every screen refresh. lazy var displayLink: CADisplayLink = { + self.displayLinkInitialized = true let display = CADisplayLink(target: TargetProxy(target: self), selector: #selector(TargetProxy.onScreenUpdate)) display.paused = true return display @@ -110,13 +114,13 @@ public class AnimatableImageView: UIImageView { /// Invalidate the displayLink so it releases its target. deinit { - // invalidate will also remove the link from all run loops - displayLink.invalidate() + if displayLinkInitialized { + displayLink.invalidate() + } } /// Attaches the display link. func attachDisplayLink() { displayLink.addToRunLoop(.mainRunLoop(), forMode: NSRunLoopCommonModes) } - }