Pages

Thursday, January 24, 2013

Login Shake effect in xCode

I have tested it and It works 


- (void)earthquake:(UIView*)itemView
{
    //AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

    CGFloat t = 5.0;

    CGAffineTransform leftQuake  = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0);
    CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0);

    itemView.transform = leftQuake;  // starting point

    [UIView beginAnimations:@"earthquake" context:itemView];//i used (__bridge void *)(itemView)
    [UIView setAnimationRepeatAutoreverses:YES]; // important
    [UIView setAnimationRepeatCount:3];
    [UIView setAnimationDuration:0.05];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)];

    itemView.transform = rightQuake; // end here & auto-reverse

    [UIView commitAnimations];
}

- (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{
    if ([finished boolValue]) 
    {
        UIView* item = (UIView *)context;//I Used (__bridge UIView *)context
        item.transform = CGAffineTransformIdentity;
   }
}
Above code reference is taken from