Skip to content

Instantly share code, notes, and snippets.

@aayusharyan
Created November 20, 2018 16:50
Show Gist options
  • Save aayusharyan/6e28349ca0ef606f16c8336e649a0b93 to your computer and use it in GitHub Desktop.
Save aayusharyan/6e28349ca0ef606f16c8336e649a0b93 to your computer and use it in GitHub Desktop.
Shake Window for invalid input OSX Swift 4 Compatible
extension NSWindow {
func shakeWindow(){
let numberOfShakes = 3
let durationOfShake = 0.3
let vigourOfShake : CGFloat = 0.04
let frame : CGRect = self.frame
let shakeAnimation :CAKeyframeAnimation = CAKeyframeAnimation()
let shakePath = CGMutablePath()
shakePath.move( to: CGPoint(x:NSMinX(frame), y:NSMinY(frame)))
for _ in 0...numberOfShakes-1 {
shakePath.addLine(to: CGPoint(x:NSMinX(frame) - frame.size.width * vigourOfShake, y:NSMinY(frame)))
shakePath.addLine(to: CGPoint(x:NSMinX(frame) + frame.size.width * vigourOfShake, y:NSMinY(frame)))
}
shakePath.closeSubpath()
shakeAnimation.path = shakePath
shakeAnimation.duration = durationOfShake
self.animations = ["frameOrigin":shakeAnimation]
self.animator().setFrameOrigin(self.frame.origin)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment