CABasicAnimation学习Demo 包括了一些常用的动画效果
来源:互联网 发布:钟表行业数据 编辑:程序博客网 时间:2024/06/05 15:08
转自:http://blog.csdn.net/feixiang_song/article/details/30972701?utm_source=tuicool&utm_medium=referral
个人写的一些例子:
-
-
-
-
-
-
-
-
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- @implementation ViewController
- @synthesize ViewTest;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
-
-
-
-
-
-
-
-
-
-
-
- [self drawACurvedLine];
-
-
-
-
- }
-
- -(void)startAnimation
- {
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
- animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
- animation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
- animation.duration = 3.0f;
- animation.repeatCount = 1;
-
-
-
- animation.fillMode = kCAFillModeForwards;
-
- [self.ViewTest.layer addAnimation:animation forKey:nil];
- }
-
- -(void)startAnimation1
- {
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
- animation.fromValue = [NSNumber numberWithFloat:0.0f];
- animation.toValue = [NSNumber numberWithFloat:10.0f];
-
-
-
-
-
-
- CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
- animation.fromValue = [NSNumber numberWithFloat:0.0f];
- animation.toValue = [NSNumber numberWithFloat:10.0f];
-
-
-
-
-
-
- CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
- groupAnimation.duration = 2.0f;
- groupAnimation.autoreverses = YES;
- groupAnimation.repeatCount = 5;
- [groupAnimation setAnimations:[NSArray arrayWithObjects:animation,animation1, nil nil]];
-
- [self.ViewTest.layer addAnimation:groupAnimation forKey:nil];
- }
-
-
- -(void)startAnimation2
- {
-
- CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
- boundsAnimation.fromValue = [NSValue valueWithCGRect: self.ViewTest.bounds];
- boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectZero];
-
- CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
- opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
- opacityAnimation.toValue = [NSNumber numberWithFloat:0.5];
-
-
- CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
- animation.fromValue = [NSValue valueWithCGPoint: self.ViewTest.layer.position];
- CGPoint toPoint = self.ViewTest.layer.position;
- toPoint.x += 180;
- animation.toValue = [NSValue valueWithCGPoint:toPoint];
-
-
- CABasicAnimation* rotationAnimation =
- [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
-
- rotationAnimation.toValue = [NSNumber numberWithFloat:(22 * M_PI) * 3];
-
-
-
-
- rotationAnimation.duration = 3.0f;
- rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
-
-
-
- CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
- scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];
- scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];
- scaleAnimation.duration = 3.0f;
- scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
-
- CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
- animationGroup.duration = 3.0f;
- animationGroup.autoreverses = YES;
- animationGroup.repeatCount = NSNotFound;
- [animationGroup setAnimations:[NSArray arrayWithObjects:rotationAnimation, scaleAnimation,boundsAnimation, nil nil]];
-
-
-
- [self.ViewTest.layer addAnimation:animationGroup forKey:@"animationGroup"];
- }
-
-
- -(void)opacityForever_Animation:(float)time
- {
- CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
- animation.fromValue=[NSNumber numberWithFloat:1.0];
- animation.toValue=[NSNumber numberWithFloat:0.0];
- animation.autoreverses=YES;
- animation.duration=time;
- animation.repeatCount=FLT_MAX;
- animation.removedOnCompletion=NO;
- animation.fillMode=kCAFillModeForwards;
-
- [self.ViewTest.layer addAnimation:animation forKey:@"opacityForever"];
- }
-
-
-
-
- -(void)opacityTimes_Animation:(float)repeatTimes durTimes:(float)time;
- {
- CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
- animation.fromValue=[NSNumber numberWithFloat:1.0];
- animation.toValue=[NSNumber numberWithFloat:0.4];
- animation.repeatCount=repeatTimes;
- animation.duration=time;
- animation.removedOnCompletion=NO;
- animation.fillMode=kCAFillModeForwards;
- animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
- animation.autoreverses=YES;
-
- [self.ViewTest.layer addAnimation:animation forKey:@"opacityTimes"];
-
- }
-
-
-
- -(void)keyframeAniamtion:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes
- {
-
- }
-
-
- -(void)drawACurvedLine
- {
- UIGraphicsBeginImageContext(CGSizeMake(320, 460));
- CGContextRef context = UIGraphicsGetCurrentContext();
-
- CGContextSetLineWidth(context, 3);
- CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
-
- CGContextMoveToPoint(context, 10, 10);
-
- CGContextAddQuadCurveToPoint(context, 10, 450, 310, 450);
-
-
-
- CGContextDrawPath(context, kCGPathStroke);
-
-
- UIImage *curve = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- UIImageView *curveView = [[UIImageView alloc]initWithImage:curve];
- curveView.frame = CGRectMake(1, 1, 320, 460);
- [curveView setBackgroundColor:[UIColor clearColor]];
- [self.view addSubview:curveView];
- }
-
- -(void)animateCicleAlongPath
- {
-
- CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
- pathAnimation.calculationMode = kCAAnimationPaced;
- pathAnimation.fillMode = kCAFillModeForwards;
- pathAnimation.removedOnCompletion = NO;
- pathAnimation.duration = 5.0f;
- pathAnimation.repeatCount = 200;
-
-
-
- CGMutablePathRef curvedPath = CGPathCreateMutable();
- CGPathMoveToPoint(curvedPath, NULL, 10, 10);
- CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 450, 310, 450);
- CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);
-
- pathAnimation.path = curvedPath;
- CGPathRelease(curvedPath);
-
- [self.ViewTest.layer addAnimation:pathAnimation forKey:nil];
-
- }
-
-
-
-
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
-
- }
-
- @end
最后附上Demo下载地址:http://download.csdn.net/detail/u012951123/7500419