Created
June 10, 2019 01:56
-
-
Save alfonsotesauro/efcb8007b09e51660cb52917924f5ee0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GlowingImageView.swift | |
// Glowing Circle | |
// | |
// Created by Alfonso Maria Tesauro on 10/06/2019. | |
// Copyright © 2019 Alfonso Maria Tesauro. All rights reserved. | |
// | |
import Cocoa | |
@IBDesignable | |
class GlowingImageView: NSImageView { | |
var animating: Bool = true | |
var displayIfStopped: Bool = true | |
override var isHidden: Bool { | |
willSet(newValue) { | |
if newValue == false { | |
self.alphaValue = 1.0 | |
} | |
} | |
} | |
override init(frame frameRect: NSRect) { | |
super.init(frame: frameRect) | |
if !animating { | |
if displayIfStopped == false { | |
self.isHidden = true | |
} | |
} else { | |
self.startAnimation() | |
} | |
} | |
required init?(coder: NSCoder) { | |
super.init(coder: coder) | |
if !animating { | |
if displayIfStopped == false { | |
self.isHidden = true | |
} | |
} else { | |
self.startAnimation() | |
} | |
} | |
// override func draw(_ dirtyRect: NSRect) { | |
// super.draw(dirtyRect) | |
// | |
// // Drawing code here. | |
// } | |
func startAnimation() { | |
self.animating = true | |
self.isHidden = false | |
self.alphaValue = 1.0 | |
self.fadeOut() | |
} | |
func stopAnimation() { | |
self.animating = false | |
if displayIfStopped == false { | |
self.isHidden = true | |
} | |
} | |
func fadeIn () { | |
if self.animating == false { | |
return | |
} | |
CATransaction.begin() | |
CATransaction.setAnimationDuration(1.2) | |
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: .easeInEaseOut)) | |
CATransaction.setCompletionBlock { | |
self.fadeOut() | |
} | |
self.animator().alphaValue = 1.0 | |
CATransaction.commit() | |
} | |
func fadeOut() { | |
if self.animating == false { | |
return | |
} | |
CATransaction.begin() | |
CATransaction.setAnimationDuration(1.2) | |
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: .easeInEaseOut)) | |
CATransaction.setCompletionBlock { | |
self.fadeIn() | |
} | |
self.animator().alphaValue = 0.3 | |
CATransaction.commit() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment