常用动画示例
1.地震动画
适合一整个大视图的突然晃动
- (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;
}
}
2.图标的抖动效果
//开始抖动
-(void)BeginWobble
{
srand([[NSDate date] timeIntervalSince1970]);
float rand=(float)random();
CFTimeInterval t=rand*0.0000000001;
[UIView animateWithDuration:0.1 delay:t options:0 animations:^
{
要抖动的视图.transform=CGAffineTransformMakeRotation(-0.05);
} completion:^(BOOL finished)
{
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction animations:^
{
要抖动的视图.transform=CGAffineTransformMakeRotation(0.05);
} completion:^(BOOL finished) {}];
}];
}
//停止抖动
-(void)EndWobble
{
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState animations:^
{
要抖动的视图.transform=CGAffineTransformIdentity;
} completion:^(BOOL finished) {}];
}