Created
November 20, 2018 16:50
-
-
Save aayusharyan/6e28349ca0ef606f16c8336e649a0b93 to your computer and use it in GitHub Desktop.
Shake Window for invalid input OSX Swift 4 Compatible
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
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