OSDN Git Service

分岐係数込みの判定式(33)の値が誤っていたので修正
[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         
29         get { return animations[.startFrame] as? NSRect }
30         set { animations[.startFrame] = newValue }
31     }
32     
33     var endFrame: NSRect? {
34         
35         get { return animations[.endFrame] as? NSRect }
36         set { animations[.endFrame] = newValue }
37     }
38     
39     var effect: NSViewAnimation.EffectName? {
40         
41         get { return animations[.effect] as? NSViewAnimation.EffectName }
42         set { animations[.effect] = newValue }
43     }
44 }
45
46 class ViewAnimation: NSViewAnimation, NSAnimationDelegate {
47     
48     var completeHandler: (() -> Void)?
49     
50     init(viewAnimations: [ViewAnimationAttributes]) {
51         
52         super.init(viewAnimations: viewAnimations.map { $0.animations })
53     }
54     
55     required init?(coder: NSCoder) {
56         
57         fatalError("Can not initialize with NSCoder")
58     }
59     
60     func start(completeHandler: @escaping () -> Void) {
61         
62         delegate = self
63         self.completeHandler = completeHandler
64         
65         start()
66     }
67     
68     ///
69     func animationDidEnd(_ animation: NSAnimation) {
70         
71         completeHandler?()
72     }
73 }