Skip to content

Instantly share code, notes, and snippets.

@alfonsotesauro
Created June 10, 2019 01:56
Show Gist options
  • Save alfonsotesauro/efcb8007b09e51660cb52917924f5ee0 to your computer and use it in GitHub Desktop.
Save alfonsotesauro/efcb8007b09e51660cb52917924f5ee0 to your computer and use it in GitHub Desktop.
//
// 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