创建一个控制器,并且定义两个宏
#import "ROOTViewController.h"
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
声明一个UIView属性
@interface ROOTViewController ()
// 声明一个UIView
@property (nonatomic, retain) UIView *myView;
@end
@implementation ROOTViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
[self addSubViews];
}
// 创建视图
- (void)addSubViews
{
self.myView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth - 100) / 2, 200, 100, 100)];
self.myView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:self.myView];
[_myView release];
// 设置圆角
// layer 是负责显示图层的
// 要更改咱们看到的图形形状,需要更改layer
// 先决条件,变圆 必须是长宽相同
self.myView.layer.cornerRadius = self.myView.frame.size.width / 2;
// 设置阴影的颜色
// CGColorRef 涂层绘制的颜色
self.myView.layer.shadowColor = [UIColor greenColor].CGColor;
// 阴影的显示范围
self.myView.layer.shadowOffset = CGSizeMake(0, 10);
// 阴影的透明度
self.myView.layer.shadowOpacity = 1;
// 阴影的模糊程度
self.myView.layer.shadowRadius = 40;
// 设置边框kuandu
self.myView.layer.borderWidth = 5;
// 设置边框的颜色
self.myView.layer.borderColor = [UIColor colorWithRed:0.533 green:0.966 blue:1.000 alpha:1.000].CGColor;
// layer 层动画
// CAPropertyAnimation 抽象类
// CABasicAnimation 基础动画,更改大小,旋转等
// CAKeyframeAnimation 主要按轨迹移动,位置,比如 执行一组动画时使用 背景颜色
// 创建button
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button1 setTitle:@"旋转" forState:(UIControlStateNormal)];
button1.frame = CGRectMake(0, 100, 100, 50);
button1.backgroundColor = [UIColor redColor];
[button1 addTarget:self action:@selector(actionButton1:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button2 setTitle:@"变大" forState:(UIControlStateNormal)];
button2.frame = CGRectMake(0, 200, 100, 50);
button2.backgroundColor = [UIColor redColor];
[button2 addTarget:self action:@selector(actionButton2:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button3 setTitle:@"改变颜色" forState:(UIControlStateNormal)];
button3.frame = CGRectMake(0, 300, 100, 50);
button3.backgroundColor = [UIColor redColor];
[button3 addTarget:self action:@selector(actionButton3:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button3];
UIButton *button4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button4 setTitle:@"轨迹移动" forState:(UIControlStateNormal)];
button4.frame = CGRectMake(0, 400, 100, 50);
button4.backgroundColor = [UIColor redColor];
[button4 addTarget:self action:@selector(actionButton4:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button4];
UIButton *button5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button5 setTitle:@"3D" forState:(UIControlStateNormal)];
button5.frame = CGRectMake(0, 500, 100, 50);
button5.backgroundColor = [UIColor redColor];
[button5 addTarget:self action:@selector(actionButton5:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button5];
UIButton *button6 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button6 setTitle:@"组动画" forState:(UIControlStateNormal)];
button6.frame = CGRectMake(0, 600, 100, 50);
button6.backgroundColor = [UIColor redColor];
[button6 addTarget:self action:@selector(actionButton6:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button6];
}
调用方法
// 旋转
- (void)actionButton1:(UIButton *)button
{
[self xyAnimation];
}
// 改变size
- (void)actionButton2:(UIButton *)button
{
[self sizeAnimation];
}
// 改变背景颜色
- (void)actionButton3:(UIButton *)button
{
[self changeBackgroundColor];
}
// 轨迹移动
- (void)actionButton4:(UIButton *)button
{
[self positionPoint];
}
// 3D旋转
- (void)actionButton5:(UIButton *)button
{
[self transform3D];
}
// 组动画
- (void)actionButton6:(UIButton *)button
{
[self groupAnimation];
}
旋转
- (void)xyAnimation
{
// 创建一个基础动画
// 注意:KeyPath一定不要拼错
// 咱们要更改的是transform.rotation.x
// 形变属性下弧度的.x轴
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
// 设置属性变化 到 什么值
// toValue 需要一个对象类型,即NSnumber或NSValue
animation.toValue = [NSNumber numberWithFloat:M_PI * 200];
// 设置动画时长
animation.duration = 0.1;
// 设置动画是否重复
animation.repeatCount = 9999;
// 把设置号的动画添加到layer上
// 参数2:添加的动画标识
[self.myView.layer addAnimation:animation forKey:@"transform.rotation.x"];
}
改变size
- (void)sizeAnimation
{
// 更改大小的话需要更改bounds.size
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
// 设置改变的值 初始值
animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(10, 10)];
// 结束值
animation.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)];
// 设置时间
animation.duration = 1;
// 设置重复次数
animation.repeatCount = 9999;
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@"bounds.size"];
}
改变背景颜色
- (void)changeBackgroundColor
{
// 可以执行一组动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// 创建绘制颜色
CGColorRef green = [UIColor greenColor].CGColor;
CGColorRef blue = [UIColor blueColor].CGColor;
CGColorRef black = [UIColor blueColor].CGColor;
CGColorRef red = [UIColor redColor].CGColor;
// 改变一组颜色
animation.values = @[(id)green, (id)black, (id)blue, (id)red];
// 设置时间
animation.duration = 3;
// 设置重复次数
animation.repeatCount = 9999;
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@"backgroundColor"];
}
轨迹移动
// 轨迹移动
- (void)positionPoint
{
// NSLog(@"%@", NSStringFromCGPoint(self.myView.layer.position));
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// 创建一堆点
NSValue *point1 = [NSValue valueWithCGPoint:CGPointMake(self.myView.center.x - 10, self.myView.center.y)];
NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(self.myView.center.x + 10, self.myView.center.y)];
NSValue *point3 = [NSValue valueWithCGPoint:CGPointMake(self.myView.center.x - 10, self.myView.center.y)];
NSValue *point4 = [NSValue valueWithCGPoint:CGPointMake(self.myView.center.x + 10, self.myView.center.y)];
NSValue *point5 = [NSValue valueWithCGPoint:CGPointMake(self.myView.center.x - 10, self.myView.center.y)];
// 设置时间
animation.duration = 0.1;
// 设置重复次数
animation.repeatCount = 9999;
// 添加点进数组
animation.values = @[point1, point2, point3, point4, point5];
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@"position"];
}
3D旋转
- (void)transform3D
{
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:@"transform"];
// 结束值
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.myView.layer.transform, M_PI, 50, 50, 50)];
// 设置时间
animation.duration = 1;
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@"transform"];
}
组动画
- (void)groupAnimation
{
// 创建组动画
CAAnimationGroup *group = [CAAnimationGroup animation];
// 可以执行一组动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// 创建绘制颜色
CGColorRef green = [UIColor greenColor].CGColor;
CGColorRef blue = [UIColor blueColor].CGColor;
CGColorRef black = [UIColor blueColor].CGColor;
CGColorRef red = [UIColor redColor].CGColor;
// 改变一组颜色
animation.values = @[(id)green, (id)black, (id)blue, (id)red];
// 更改大小的话需要更改bounds.size
CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
// 设置改变的值 初始值
animation1.fromValue = [NSValue valueWithCGSize:CGSizeMake(10, 10)];
// 结束值
animation1.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)];
// 设置组动画时间
group.duration = 1;
// 设置组动画执行的动画数组
group.animations = @[animation, animation1];
// 添加到layer上
[self.myView.layer addAnimation:group forKey:@"group"];
}