抖动特效 shaking animation

- (void)earthquake:(UIView*)itemView

{

// AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

CGFloat t = 2.0;

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

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

itemView.transform = leftQuake; // starting point

[UIView beginAnimations:@"earthquake"context: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;

//每次变换前都要置位,不然你变换用的坐标系统不是屏幕坐标系统(即绝对坐标系统),而是上一次变换后的坐标系统

item.transform = CGAffineTransformIdentity;

}

}



方法二 利用块语法


- (void)shakeView:(UIView *)viewToShake { CGFloat t = 2.0; CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0); CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0); viewToShake.transform = translateLeft; [UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{ [UIView setAnimationRepeatCount:2.0]; viewToShake.transform = translateRight; } completion:^(BOOL finished) { if (finished) { [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ viewToShake.transform = CGAffineTransformIdentity; } completion:NULL]; } }]; 

}


你可能感兴趣的:(animation)