             // Some devices (like iPad Pro 10.5) will have a different FPS.
             duration = 1.0 / Double(displayLink.preferredFramesPerSecond)
         }
-    
-        if animator?.updateCurrentFrame(duration: duration) ?? false {
-            layer.setNeedsDisplay()
-
-            if animator?.isReachMaxRepeatCount ?? false {
-                stopAnimating()
-                delegate?.animatedImageViewDidFinishAnimating(self)
-            }
-        }
-    }
+
+        animator.shouldChangeFrame(with: duration) { hasNewFrame in
+            if hasNewFrame {
+                layer.setNeedsDisplay()
+            }
+        }
+    }
+}
+
+protocol AnimatorDelegate: AnyObject {
+    func animator(_ animator: AnimatedImageView.Animator, didPlayAnimationLoops count: UInt)
 }
 
 extension AnimatedImageView: AnimatorDelegate {

        }
        
        @objc func onScreenUpdate() {
            target?.updateFrame()
        }
    }


        return indices ~= index ? self[index] : nil
    }
}
