OSDN Git Service

ShipSlotObserverを使用するように変更
[kcd/KCD.git] / KCD / ViewAnimation.swift
1 //
2 //  ViewAnimation.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/12/23.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 protocol ViewAnimationTerget {}
12 extension NSView: ViewAnimationTerget {}
13 extension NSWindow: ViewAnimationTerget {}
14
15 struct ViewAnimationAttributes {
16     
17     private(set) var animations: [NSViewAnimation.Key: Any]
18     
19     init(target: ViewAnimationTerget, startFrame: NSRect? = nil, endFrame: NSRect? = nil, effect: NSViewAnimation.EffectName? = nil ) {
20         
21         animations = [.target: target]
22         animations[.startFrame] = startFrame
23         animations[.endFrame] = endFrame
24         animations[.effect] = effect
25     }
26     
27     var startFrame: NSRect? {
28         get { return animations[.startFrame] as? NSRect }
29         set { animations[.startFrame] = newValue }
30     }
31     
32     var endFrame: NSRect? {
33         get { return animations[.endFrame] as? NSRect }
34         set { animations[.endFrame] = newValue }
35     }
36     
37     var effect: NSViewAnimation.EffectName? {
38         get { return animations[.effect] as? NSViewAnimation.EffectName }
39         set { animations[.effect] = newValue }
40     }
41 }
42
43 class ViewAnimation: NSViewAnimation, NSAnimationDelegate {
44     
45     var completeHandler: (() -> Void)?
46     
47     init(viewAnimations: [ViewAnimationAttributes]) {
48         
49         super.init(viewAnimations: viewAnimations.map { $0.animations })
50     }
51     
52     required init?(coder: NSCoder) {
53         fatalError("Can not initialize with NSCoder")
54     }
55     
56     func start(completeHandler: @escaping () -> Void) {
57         
58         delegate = self
59         self.completeHandler = completeHandler
60         
61         start()
62     }
63     
64     ///
65     func animationDidEnd(_ animation: NSAnimation) {
66         
67         completeHandler?()
68     }
69 }