Skip to content

Instantly share code, notes, and snippets.

@valexa
Created December 29, 2009 16:12
Show Gist options
  • Save valexa/265394 to your computer and use it in GitHub Desktop.
Save valexa/265394 to your computer and use it in GitHub Desktop.
- (CAKeyframeAnimation *)negativeShake:(NSRect)frame{
int numberOfShakes = 4;
float durationOfShake = 0.5f;
float vigourOfShake = 0.05f;
CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animation];
CGMutablePathRef shakePath = CGPathCreateMutable();
CGPathMoveToPoint(shakePath, NULL, NSMinX(frame), NSMinY(frame));
int index;
for (index = 0; index < numberOfShakes; ++index)
{
CGPathAddLineToPoint(shakePath, NULL, NSMinX(frame) - frame.size.width * vigourOfShake, NSMinY(frame));
CGPathAddLineToPoint(shakePath, NULL, NSMinX(frame) + frame.size.width * vigourOfShake, NSMinY(frame));
}
CGPathCloseSubpath(shakePath);
shakeAnim.path = shakePath;
shakeAnim.duration = durationOfShake;
return shakeAnim;
}
- (IBAction)shakeBabyShake:(id)sender;
{
NSString *key = @"frameOrigin";
#if __i386__
if ([NSView defaultAnimationForKey:key] == nil){
#elif __x86_64__
if ([NSWindow defaultAnimationForKey:key] == nil){
#endif
NSLog(@"NSVindow not animatable for key '%@'",key);
}else {
[[NSApp keyWindow] setAnimations:[NSDictionary dictionaryWithObject:[self negativeShake:[[NSApp keyWindow] frame]] forKey:key]];
[[[NSApp keyWindow] animator] setFrameOrigin:[[NSApp keyWindow] frame].origin];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment