Thursday, 11 October 2012

Auto iPhone Shaking | UIView shaking


Tutorial for shake your UIView. It may be your UIButton, UITextField, UISegmentControl or any more.

Below are the variables used in our piece of code.
1) kDuratonTime -> your animation speed duration time.
2) direction -> for co-ordinates to move around.
3) shakes -> for how many times to shake your view.

#define kDurationTime .05
int direction = 1;
int shakes = 0;

-(void)shake:(UIView *)shakeView{
   [UIView animateWithDuration:kDurationTime animations:^{
              shakeView.transform = CGAffineTransformMakeTranslation(5 *direction,0);
         }
           completion:^(BOOL finished){
           if(shakes >= 10){
              shakeView.transform = CGAffineTransformIdentity;
              return;
              } 
          shakes++;
          direction = direction * -1;
          [self shake:shakeView];
        }];
}
Calling this function like:  [self shake:YOUR_VIEW];
  

No comments:

Post a Comment