iOS View 抖动效果
2016-08-15 本文已影响180人
Ever_Blacks
在最近的项目中使用到View抖动的效果,就从StackOverflow搬运了一个在此记录一下。
- (void)earthquake:(UIView*)itemView {
CGFloat t = 2.0;
CGAffineTransform leftQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0);
CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0); //水平晃动
itemView.transform = leftQuake; // starting point
[UIView beginAnimations:@"earthquake" context:(__bridge void *)(itemView)];
[UIView setAnimationRepeatAutoreverses:YES]; // 如果不加这一句 整个动画感觉不连贯
[UIView setAnimationRepeatCount:5];
[UIView setAnimationDuration:0.07];
[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 = (__bridge UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}