自定义UIView动画效果
最普通动画:
1 //开始动画 2 [UIView beginAnimations:nil context:nil]; 3 //设定动画持续时间 4 [UIView setAnimationDuration:2]; 5 //动画的内容 6 frame.origin.x += 150; 7 [img setFrame:frame]; 8 //动画结束 9 [UIView commitAnimations];
连续动画:一个接一个地显示一系列的图像
1 NSArray *myImages = [NSArray arrayWithObjects: 2 [UIImage imageNamed:@"myImage1.png"], 3 [UIImage imageNamed:@"myImage2.png"], 4 [UIImage imageNamed:@"myImage3.png"], 5 [UIImage imageNamed:@"myImage4.gif"], nil]; 6 7 UIImageView *myAnimatedView = [UIImageView alloc]; 8 [myAnimatedView initWithFrame:[self bounds]]; 9 myAnimatedView.animationImages = myImages; //animationImages属性返回一个存放动画图片的数组 10 myAnimatedView.animationDuration = 0.25; //浏览整个图片一次所用的时间 11 myAnimatedView.animationRepeatCount = 0; // 0 = loops forever 动画重复次数 12 [myAnimatedView startAnimating]; 13 [self addSubview:myAnimatedView]; 14 [myAnimatedView release]; 15 16 CATransition Public API动画: 17 CATransition *animation = [CATransition animation]; 18 //动画时间 19 animation.duration = 0.5f; 20 //先慢后快 21 animation.timingFunction = UIViewAnimationCurveEaseInOut; 22 animation.fillMode = kCAFillModeForwards; 23 //animation.removedOnCompletion = NO;
各种动画效果
1 /* 2 kCAFillModeRemoved 这个是默认值,也就是说当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态 3 4 kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态 5 kCAFillModeBackwards 这个和kCAFillModeForwards是相对的,就是在动画开始前,你只要将动画加入了一个layer,layer便立即进入动画的初始状态并等待动画开始.你可以这样设定测试代码,将一个动画加入一个layer的时候延迟5秒执行.然后就会发现在动画没有开始的时候,只要动画被加入了layer,layer便处于动画初始状态 6 kCAFillModeBoth 理解了上面两个,这个就很好理解了,这个其实就是上面两个的合成.动画加入后开始之前,layer便处于动画初始状态,动画结束后layer保持动画最后的状态. 7 8 */ 9 /* 10 kCATransitionFromRight; 11 kCATransitionFromLeft; 12 kCATransitionFromTop; 13 kCATransitionFromBottom; 14 */ 15 //各种组合 16 animation.type = kCATransitionPush; 17 animation.subtype = kCATransitionFromRight; 18 19 [self.view.layer addAnimation:animation forKey:@"animation"]; 20 21 CATransition Private API动画: 22 animation.type可以设定为以下效果 23 动画效果汇总: 24 /* 25 suckEffect(三角) 26 27 rippleEffect(水波抖动) 28 29 pageCurl(上翻页) 30 31 pageUnCurl(下翻页) 32 33 oglFlip(上下翻转) 34 35 cameraIris/cameraIrisHollowOpen/cameraIrisHollowClose (镜头快门,这一组动画是有效果,只是很难看,不建议使用 36 37 而以下为则黑名单: 38 39 spewEffect: 新版面在屏幕下方中间位置被释放出来覆盖旧版面. 40 41 - genieEffect: 旧版面在屏幕左下方或右下方被吸走, 显示出下面的新版面 (阿拉丁灯神?). 42 43 - unGenieEffect: 新版面在屏幕左下方或右下方被释放出来覆盖旧版面. 44 45 - twist: 版面以水平方向像龙卷风式转出来. 46 47 - tubey: 版面垂直附有弹性的转出来. 48 49 - swirl: 旧版面360度旋转并淡出, 显示出新版面. 50 51 - charminUltra: 旧版面淡出并显示新版面. 52 53 - zoomyIn: 新版面由小放大走到前面, 旧版面放大由前面消失. 54 55 - zoomyOut: 新版面屏幕外面缩放出现, 旧版面缩小消失. 56 57 - oglApplicationSuspend: 像按"home" 按钮的效果. 58 */ 59 60 UIView Animations 动画: 61 [UIView beginAnimations:@"animationID" context:nil]; 62 [UIView setAnimationDuration:0.5f]; 63 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 64 [UIView setAnimationRepeatAutoreverses:NO]; 65 //以下四种效果 66 /* 67 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];//oglFlip, fromLeft 68 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];//oglFlip, fromRight 69 [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; 70 [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; 71 */ 72 73 [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; 74 [UIView commitAnimations]; 75 IOS4.0新方法: 76 方法: +(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations; 77 + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; //多一个动画结束后可以执行的操作. 78 //下边是嵌套使用,先变大再消失的动画效果. 79 [UIView animateWithDuration:1.25 animations:^{ 80 CGAffineTransform newTransform = CGAffineTransformMakeScale(1.2, 1.2); 81 [firstImageView setTransform:newTransform]; 82 [secondImageView setTransform:newTransform];} 83 completion:^(BOOL finished){ 84 [UIView animateWithDuration:1.2 animations:^{ 85 [firstImageView setAlpha:0]; 86 [secondImageView setAlpha:0];} completion:^(BOOL finished){ 87 [firstImageView removeFromSuperview]; 88 [secondImageView removeFromSuperview]; }]; 89 }];