#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIView *greenView;
@property (weak, nonatomic) IBOutlet UIView *blueView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)animationAction1:(UIButton *)sender {
NSLog(@"属性动画");
//开始动画
[UIView beginAnimations:@"属性动画" context:nil];
//设置动画持续时间
[UIView setAnimationDuration:2];
//设置动画延迟时间
// [UIView setAnimationDelay:2];
//设置重复次数
[UIView setAnimationRepeatCount:5];
//设置自动翻转
[UIView setAnimationRepeatAutoreverses:YES];
//设置过渡效果
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//设置代理
[UIView setAnimationDelegate:self];
//给动画添加事件(一定要先设置代理)
[UIView setAnimationWillStartSelector:@selector(startAnimation)];
// [UIView setAnimationWillStartSelector:<#(nullable SEL)#>:@selector(stopAnimation)];
// _redView.center = CGPointMake(300, 150);
_redView.center = CGPointMake(300, 600);
//结束动画
[UIView commitAnimations];
}
- (IBAction)animationAction2:(UIButton *)sender {
NSLog(@"Block动画");
// [UIView animateWithDuration:1 animations:^{
// //延期
// [UIView setAnimationDelay:1];
// //动画进行时需要的操作
// _redView.backgroundColor = [UIColor yellowColor];
// }];
// [UIView animateWithDuration:2 animations:^{
// //进行时
//
// } completion:^(BOOL finished) {
// //完成时
//
// }];
[UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionAutoreverse animations:^{
[UIView setAnimationRepeatCount:4];
} completion:^(BOOL finished) {
}];
}
- (IBAction)UIViewTransition:(UIButton *)sender {
NSLog(@"视图切换 过度 效果");
//和UI第三节容器试图控制器切换子视图控制器的方法类似
// [UIView transitionFromView:_blueView toView:_greenView duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {
// NSLog(@"转换蓝绿视图成功");
// }];
[UIView transitionWithView:_greenView duration:1 options:UIViewAnimationOptionAutoreverse animations:^{
_blueView.backgroundColor = [UIColor blackColor];
} completion:^(BOOL finished) {
_blueView.backgroundColor = [UIColor blueColor];
}];
}
- (IBAction)DCGAffineTransfrom:(UIButton *)sender {
NSLog(@"2D仿射变换");
//开始动画
[UIView beginAnimations:@"2D仿射变换" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationRepeatCount:8];
#pragma mark ---旋转---
// //基于原始
//// _redView.transform = CGAffineTransformMakeRotation(M_PI_2);
//
//
// //基于上一次
// _redView.transform = CGAffineTransformRotate(_redView.transform,M_PI_2/3);
//
//
// //结束动画
// [UIView commitAnimations];
#pragma mark ---- 缩放 ---
// _redView.transform = CGAffineTransformMakeScale(2, 0.5);
_redView.transform = CGAffineTransformMake(-1, cos(M_PI_2/3), 3, sin(M_PI), 1, 1);
//结束动画
[UIView commitAnimations];
}
- (IBAction)basicAction:(UIButton *)sender {
NSLog(@"basic");
/*
// CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds.size"];//引号内的字母不能错;
//
//
//
// CGSize fromSize = CGSizeMake(100, 100);
// CGSize toSize = CGSizeMake(20, 200);
//
// //设置初始值
// basic.fromValue = [NSValue valueWithCGSize:fromSize];
// //设置结束值
// basic.toValue = [NSValue valueWithCGSize:toSize];
// //动画只是一个效果,不会改变属性的值若想改变属性的值,需要手动改变
// CGRect newRect = _redView.layer.bounds;
// newRect.size = toSize;
// _redView.layer.bounds = newRect;
//
// [_redView.layer addAnimation:basic forKey:@"test"];
*/
/*
CABasicAnimation *basic1 = [CABasicAnimation animationWithKeyPath:@"frame"];
CGRect fromFrame = CGRectMake(10, 10, 100, 100);
CGRect toFrame = CGRectMake(50, 10, 50, 150);
basic1.fromValue = [NSValue valueWithCGRect:fromFrame];
basic1.toValue = [NSValue valueWithCGRect:toFrame];
CGRect newrect = _redView.layer.bounds;
_redView.layer.bounds = newrect;
[_redView.layer addAnimation:basic1 forKey:@"test1"];
*/
//取出redvview的layer层
CALayer *myLayer = _redView.layer;
//获取layer的位置
CGPoint position = myLayer.position;
//设置晃动时两个终点的位置
CGPoint x1 = CGPointMake(position.x-50, position.y);
CGPoint x2 = CGPointMake(position.x+50, position.y);
//设置动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
//设置开始位置
[animation setFromValue:[NSValue valueWithCGPoint:x1]];
//设置结束位置
[animation setToValue:[NSValue valueWithCGPoint:x2]];
//设置自动反转
[animation setAutoreverses:YES];
//设置持续时间
[animation setDuration:0.06];
//设置重复次数
[animation setRepeatCount:4];
//添加动画
[myLayer addAnimation:animation forKey:@"test"];
}
- (IBAction)keyFrameAction:(UIButton *)sender {
NSLog(@"keyFrame");
//创建并制定路径
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置运动轨迹
keyFrame.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(50, 170)],[NSValue valueWithCGPoint:CGPointMake(364, 170)] , [NSValue valueWithCGPoint:CGPointMake(110 , 364)],[NSValue valueWithCGPoint:CGPointMake(207, 50)],[NSValue valueWithCGPoint:CGPointMake(300, 364)],[NSValue valueWithCGPoint:CGPointMake(50, 170)],nil];
//设置持续时间
keyFrame.duration = 12;
//设置关键帧时间
keyFrame.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.1], [NSNumber numberWithFloat:0.2],[NSNumber numberWithFloat:0.3], [NSNumber numberWithFloat:0.4], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:0.6], nil];
//添加动画
[_redView.layer addAnimation:keyFrame forKey:@"test1"];
}
- (IBAction)groupAction:(UIButton *)sender {
NSLog(@"group");
CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"bounds.size"];//引号内的字母不能错;
CGSize fromSize = CGSizeMake(100, 100);
CGSize toSize = CGSizeMake(20, 200);
//设置初始值
basic.fromValue = [NSValue valueWithCGSize:fromSize];
//设置结束值
basic.toValue = [NSValue valueWithCGSize:toSize];
//动画只是一个效果,不会改变属性的值若想改变属性的值,需要手动改变
CGRect newRect = _redView.layer.bounds;
newRect.size = toSize;
_redView.layer.bounds = newRect;
//创建并制定路径
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置运动轨迹
keyFrame.values = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(50, 170)],[NSValue valueWithCGPoint:CGPointMake(364, 170)] , [NSValue valueWithCGPoint:CGPointMake(110 , 364)],[NSValue valueWithCGPoint:CGPointMake(207, 50)],[NSValue valueWithCGPoint:CGPointMake(300, 364)],[NSValue valueWithCGPoint:CGPointMake(50, 170)],nil];
//设置持续时间
keyFrame.duration = 6;
//设置关键帧时间
keyFrame.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.1], [NSNumber numberWithFloat:0.2],[NSNumber numberWithFloat:0.3], [NSNumber numberWithFloat:0.4], [NSNumber numberWithFloat:0.5], [NSNumber numberWithFloat:0.6], nil];
//创建Group动画
CAAnimationGroup *group = [CAAnimationGroup animation];
//设置持续时间
group.duration = 3;
//将动画添加到数组
group.animations = @[basic, keyFrame];
[_redView.layer addAnimation:group forKey:@"test2"];
}
- (IBAction)CATransitionAction:(UIButton *)sender {
NSLog(@"CATransition");
//创建layer动画
CATransition *sition = [CATransition animation];
//
sition.duration = 2;
//
sition.subtype = kCATransitionFromBottom;
//设置过渡效果
//1.系统提供样式
// sition.type = kCATransitionPush;
//2.私有API(最好不用,有上线被拒的风险)
sition.type = @"cameraIrisHollowOpen";
//添加动画
[_redView.layer addAnimation:sition forKey:@"test"];
}
/*
以下是基本的四种效果
kCATransitionPush 推入效果
kCATransitionMoveIn 移入效果
kCATransitionReveal 截开效果
kCATransitionFade 渐入渐出效果
以下API效果可以安全使用
cube 方块
suckEffect 三角
rippleEffect 水波抖动
pageCurl 上翻页
pageUnCurl 下翻页
oglFlip 上下翻转
cameraIrisHollowOpen 镜头快门开
cameraIrisHollowClose 镜头快门开
以下API效果请慎用
spewEffect 新版面在屏幕下方中间位置被释放出来覆盖旧版面.
genieEffect 旧版面在屏幕左下方或右下方被吸走, 显示出下面的新版面
unGenieEffect 新版面在屏幕左下方或右下方被释放出来覆盖旧版面.
twist 版面以水平方向像龙卷风式转出来.
tubey 版面垂直附有弹性的转出来.
swirl 旧版面360度旋转并淡出, 显示出新版面.
charminUltra 旧版面淡出并显示新版面.
zoomyIn 新版面由小放大走到前面, 旧版面放大由前面消失.
zoomyOut 新版面屏幕外面缩放出现, 旧版面缩小消失.
oglApplicationSuspend 像按”home” 按钮的效果.
*/
-(void)startAnimation{
NSLog(@"开始动画");
}
-(void)stopAnimation{
NSLog(@"停止动画");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS--Animation动画
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- tags: Animation 上篇iOS animation动画三个角色(上)介绍了主角CALayer和几个动画...
- 还可以参考这篇:http://www.cnblogs.com/shenfangfang/p/5713564.htm...
- Core Animation - 基础动画 CAAnimation:核心动画的基础类(不能直接使用),负责动画运行...