From fbe2d639811d61e1588026f3c6f407b678262895 Mon Sep 17 00:00:00 2001 From: Alan Tai Date: Fri, 16 Nov 2018 12:53:51 +0800 Subject: [PATCH] Fix Core Animation implementation due to changes in Mojave layer-backing --- DevExcuses/Sources/DevExcusesView.swift | 2 +- DevExcuses/Sources/KenBurnsView.swift | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/DevExcuses/Sources/DevExcusesView.swift b/DevExcuses/Sources/DevExcusesView.swift index 498f3a7..c1523f7 100644 --- a/DevExcuses/Sources/DevExcusesView.swift +++ b/DevExcuses/Sources/DevExcusesView.swift @@ -318,7 +318,6 @@ class DevExcusesView: ScreenSaverView { private func updateImageView(data: Data) { let imageView = KenBurnsView(frame: self.frame) - self.addSubview(imageView) imageView.animate( image : NSImage(data: data), @@ -329,6 +328,7 @@ class DevExcusesView: ScreenSaverView { oldImageView.removeFromSuperview() } + self.addSubview(imageView) self.imageView = imageView } diff --git a/DevExcuses/Sources/KenBurnsView.swift b/DevExcuses/Sources/KenBurnsView.swift index 101699b..52b9ee6 100644 --- a/DevExcuses/Sources/KenBurnsView.swift +++ b/DevExcuses/Sources/KenBurnsView.swift @@ -3,15 +3,21 @@ import Cocoa final class KenBurnsView: NSImageView { private let configs = Configs() + private var duration: TimeInterval = 15 + func animate(image: NSImage?, alpha: CGFloat, duration: TimeInterval) { - self.image = image + self.image = image + self.alphaValue = alpha + self.duration = duration + } - guard let image = self.image else { - return - } + override var wantsUpdateLayer: Bool { + return true + } - let width = Double(image.size.width) - let height = Double(image.size.height) + override func makeBackingLayer() -> CALayer { + let width = Double(self.frame.size.width) + let height = Double(self.frame.size.height) let maxScale = Double(self.configs.maxZoom) / 100 let fromScale = Double.random(min: 1, max: maxScale) let toScale = Double.random(min: 1, max: maxScale) @@ -40,8 +46,7 @@ final class KenBurnsView: NSImageView { layer.add(xAnimation, forKey: xAnimation.keyPath) layer.add(yAnimation, forKey: yAnimation.keyPath) - self.layer = layer - self.alphaValue = alpha + return layer } private static func randomTranslation(min: Double, max: Double, scale: Double) -> Double {