概要
一些简单的动画代理学习例子,包括显示、隐式、关键帧、关键帧路径四类动画。(感觉这个动画太复杂,学习简单的例子没继续了)
结果展示
流程概要
见代码
主要代码
// // ViewController.m // Animation // // Created by arbboter on 14/12/20. // Copyright (c) 2014年 arbboter. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic, retain) UIImageView* head; @property (nonatomic, retain) UIButton* implicitAnimation; @property (nonatomic, retain) UIButton* explicitAnimation; @property (nonatomic, retain) UIButton* keyFrameAnimation; @property (nonatomic, retain) UIButton* resetHeatState; @end @implementation ViewController - (void)resetHead { [_head removeFromSuperview]; [_head release]; _head = nil; _head = [[UIImageView alloc] init]; _head.image = [UIImage imageNamed:@"head.png"]; _head.contentMode = UIViewContentModeScaleAspectFit; [self.view addSubview:_head]; /** 设置初始值小且半透明 */ _head.frame = CGRectMake(10, 20, _head.image.size.width, _head.image.size.height); _head.transform = CGAffineTransformMakeScale(0.1, 0.1); _head.center = CGPointMake(self.view.center.x, _head.frame.size.height/2+20); _head.alpha = 0.03; } - (void)viewDidLoad { [super viewDidLoad]; [self resetHead]; UIButton* btn = nil; /** 重置按钮 */ btn = [[UIButton alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height-40, 80, 30)]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitle:@"重置动画" forState:UIControlStateNormal]; btn.layer.borderWidth = 1; btn.layer.cornerRadius = 10; [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; _resetHeatState = btn; /** 隐式动画按钮 */ btn = [[UIButton alloc] initWithFrame:CGRectMake(_resetHeatState.frame.origin.x + _resetHeatState.frame.size.width + 10, self.view.frame.size.height-40, 80, 30)]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitle:@"隐式动画" forState:UIControlStateNormal]; btn.layer.borderWidth = 1; btn.layer.cornerRadius = 10; [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; _implicitAnimation = btn; /** 显示动画按钮 */ btn = [[UIButton alloc] initWithFrame:CGRectMake(_implicitAnimation.frame.origin.x + _implicitAnimation.frame.size.width + 10, self.view.frame.size.height-40, 80, 30)]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitle:@"显示动画" forState:UIControlStateNormal]; btn.layer.borderWidth = 1; btn.layer.cornerRadius = 10; [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; _explicitAnimation = btn; /** 关键帧动画按钮 */ btn = [[UIButton alloc] initWithFrame:CGRectMake(_explicitAnimation.frame.origin.x + _explicitAnimation.frame.size.width + 10, self.view.frame.size.height-40, 80, 30)]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn setTitle:@"关键帧" forState:UIControlStateNormal]; btn.layer.borderWidth = 1; btn.layer.cornerRadius = 10; [btn addTarget:self action:@selector(onAnimate:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; _keyFrameAnimation = btn; } -(void)onAnimate:(id)sender { CGPoint headCenter = _head.center; CGPoint viewCenter = self.view.center; if (sender == _implicitAnimation) { /** 隐式动画 */ [UIView beginAnimations:nil context:NULL]; _head.layer.affineTransform = CGAffineTransformMakeTranslation(0, viewCenter.y*3/2-headCenter.y); _head.layer.affineTransform = CGAffineTransformScale(_head.layer.affineTransform, 1, 1); _head.layer.opacity = 1; [UIView commitAnimations]; } else if(sender == _explicitAnimation) { /** 显示动画 */ /** 添加第一个动画【由透明渐变为清晰】 使用内置动画效果:opacity */ CABasicAnimation *opAnim = [CABasicAnimation animationWithKeyPath:@"opacity"]; opAnim.duration = 5.0; opAnim.fromValue = [NSNumber numberWithFloat:0.1]; opAnim.toValue= [NSNumber numberWithFloat:1.0]; //opAnim.cumulative = YES; opAnim.repeatCount = 1; [_head.layer addAnimation:opAnim forKey:@"animateOpacity"]; /** 添加第二个动画 平移而且变大 使用内置动画效果:transform */ CGAffineTransform t = CGAffineTransformMakeTranslation(0, viewCenter.y*3/2-headCenter.y); t = CGAffineTransformScale(t, 1.5, 1.5); CABasicAnimation *animatation = [CABasicAnimation animationWithKeyPath:@"transform"]; animatation.duration = opAnim.duration; animatation.toValue= [NSValue valueWithCATransform3D: CATransform3DMakeAffineTransform(t)]; [_head.layer addAnimation:animatation forKey:@"animateTransform"]; } else if(sender == _keyFrameAnimation) { /** 关键帧动画 */ if(arc4random()%2) { CAKeyframeAnimation *opAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; opAnim.duration = 6.0; opAnim.values =[NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.1], [NSNumber numberWithFloat:0.3], [NSNumber numberWithFloat:1.0], nil]; opAnim.keyTimes = [ NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.3], [NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:1.0], nil]; [_head.layer addAnimation:opAnim forKey:@"keyAnimateOpacity"]; CGAffineTransform t = CGAffineTransformMakeTranslation(0, viewCenter.y*3/2-headCenter.y); t = CGAffineTransformScale(t, 1.5, 1.5); CABasicAnimation *animatation = [CABasicAnimation animationWithKeyPath:@"transform"]; animatation.duration = opAnim.duration; animatation.toValue= [NSValue valueWithCATransform3D: CATransform3DMakeAffineTransform(t)]; [_head.layer addAnimation:animatation forKey:@"keyAnimateTransform"]; } else { _head.alpha = 1.0; /** 关键帧之路径动画 */ NSInteger x = self.view.frame.size.width; NSInteger y = self.view.frame.size.height; int n = 4; CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, headCenter.x, headCenter.y); for (int i=0; i<n; i++) { CGPathAddLineToPoint(path, NULL, 0, arc4random()%y); CGPathAddLineToPoint(path, NULL, x, arc4random()%y); } CGPathCloseSubpath(path); CAKeyframeAnimation *animation = nil; animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; [animation setDuration:n*5]; [animation setDelegate:self]; [animation setPath:path]; CFRelease(path); path = nil; [[_head layer] addAnimation:animation forKey:@"position"]; } } else if(sender == _resetHeatState) { [self resetHead]; } } - (void)dealloc { [_head release]; [_implicitAnimation release]; [_explicitAnimation release]; [_resetHeatState release]; [super dealloc]; } @end
项目工程
时间: 2024-10-11 19:26:41