//
// RootViewController.m
// LessonUIView-03
//
// Created by lanou3g on 15/9/23.
// Copyright (c) 2015年 山神. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
@property (nonatomic, retain) UIView *myView;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addSomeViews];
}
// 创建视图
- (void)addSomeViews
{
self.myView = [[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease];
self.myView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.myView];
[self.myView release];
// layer是负责显示图层的
// 要更改咱们看到的图形形状需要更改layer
// 设置圆角
self.myView.layer.cornerRadius = self.myView.frame.size.width / 2;
// 设置阴影颜色
// CGColorRef涂层绘制的颜色
self.myView.layer.shadowColor = [UIColor blackColor].CGColor;
self.myView.layer.shadowOffset = CGSizeMake(10, 10); // 阴影显示范围
self.myView.layer.shadowOpacity = 1; // 阴影的透明度
self.myView.layer.shadowRadius = 50; // 设置阴影的模糊程度
// 设置边框
self.myView.layer.borderWidth = 1;
// 设置边框的颜色
self.myView.layer.borderColor = [UIColor redColor].CGColor;
// layer层动画
// CAPropertyAnimation 抽象类
// CABasicAnimation 基础动画, 更改大小, 旋转等.
// CAKeyframeAnimation 主要实现按轨迹移动, 位置; 比如执行一组动画时使用, 背景颜色
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 300, 100, 50);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
// 旋转
- (void)xyAnimation
{
// 创建一个基础动画
// 注意KeyPath一定不要拼错
// 要更改的是transform.rotation.x
// 形变属性下, 弧度的点x轴
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
// 设置属性变化
animation.toValue = [NSNumber numberWithFloat:M_PI];
// 设置动画时间
animation.duration = 1;
// 设置动画重复
animation.repeatCount = 1;
// 把设置好的动画添加到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;
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@"boundss.size"];
}
// 改变颜色
- (void)changeBackgroundColor
{
// 执行一组动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// 创建绘制颜色
CGColorRef green = [UIColor greenColor].CGColor;
CGColorRef red = [UIColor redColor].CGColor;
CGColorRef blue = [UIColor blueColor].CGColor;
CGColorRef yellow = [UIColor yellowColor].CGColor;
NSArray *array = @[(id)green, (id)red, (id)blue, (id)yellow];
// 改变一组颜色
animation.values = array;
// 设置时间
animation.duration = 1;
// 添加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(150, 150)];
NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(100, 150)];
NSValue *point3 = [NSValue valueWithCGPoint:CGPointMake(200, 150)];
NSValue *point4 = [NSValue valueWithCGPoint:CGPointMake(150, 150)];
NSValue *point5 = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
// 设置时间
animation.duration = 0.1;
// 添加进数组
// animation.values = @[point1, point2, point3, point4, point5, point2, point1, point4, point3];
animation.repeatCount = 10;
animation.values = @[point1, point2, point3, point4];
// 添加到layer上
[self.myView.layer addAnimation:animation forKey:@"position"];
}
// 轨迹晃动
- (void)huangdong
{
}
// 3D 旋转
- (void)transform3D
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
// 结束值
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.myView.layer.transform, M_PI, 12, 12, 12)];
animation.duration = 1;
[self.myView.layer addAnimation:animation forKey:@"transform"];
}
// 组动画
- (void)groupAnimation
{
// 创建组动画
CAAnimationGroup *group = [CAAnimationGroup animation];
// 设置组动画时间
group.duration = 3;
// 设置组动画执行的动画数组
// 数组中
CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform"];
// 结束值
animation1.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.myView.layer.transform, M_PI, 12, 12, 12)];
[self.myView.layer addAnimation:animation1 forKey:@"transform"];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// 创建绘制颜色
CGColorRef green = [UIColor greenColor].CGColor;
CGColorRef red = [UIColor redColor].CGColor;
CGColorRef blue = [UIColor blueColor].CGColor;
CGColorRef yellow = [UIColor yellowColor].CGColor;
NSArray *array = @[(id)green, (id)red, (id)blue, (id)yellow];
// 改变一组颜色
animation.values = array;
CAKeyframeAnimation *animation2 = animation;
group.animations = @[animation1, animation2];
[self.myView.layer addAnimation:group forKey:@"group"];
}
- (void)actionButton:(UIButton *)button
{
// 旋转
[self groupAnimation];
}