iOS开发 UI--动画
2016-01-07 21:33
351 查看
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UIView *myView;
//转场动画 切换的视图
@property (nonatomic,strong)UIView *firstView;
@property (nonatomic,strong)UIView *secondView;
//转场动画 标记是否正在进行动画
@property (nonatomic,assign)BOOL isAnimating;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
动画:1.UIView自带动画效果
2.Layer层动画
3.CGAffineTransform2D变换
4.核心动画
*/
self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.myView.backgroundColor = [UIColor cyanColor];
//圆角
// self.myView.layer.cornerRadius =50;
// self.myView.layer.masksToBounds = YES;
[self.view addSubview:self.myView];
//CAAnimation
// self.myView.layer.position =CGPointMake(0, 0);
// self.myView.layer.anchorPoint = CGPointMake(0,0);
// Layer 图层
/* CALayer *layer = [[CALayer alloc] init];
//
layer.frame = CGRectMake(150, 300, 80, 80);
layer.backgroundColor = [UIColor yellowColor].CGColor;
//layer的属性
//layer.masksToBounds = YES;
layer.cornerRadius = 40;
//阴影
layer.shadowColor = [UIColor redColor].CGColor;
//偏移量
layer.shadowOffset = CGSizeMake(10, 10);
//透明程度
layer.shadowOpacity = 1;
//雨化效果
layer.shadowRadius = 30;
//锚点和位置
layer.position = CGPointMake(100, 200);//位置,将锚点对应的指定的点上
layer.anchorPoint = CGPointMake(0, 0);//锚点,基准点,旋转和缩放以该点为基准点
[self.view.layer addSublayer:layer];;
*/
//转场动画 初始化两个视图
/* self.firstView = [[UIView alloc] init];
self.firstView.frame = self.myView.bounds;
self.firstView.backgroundColor = [UIColor blueColor];
[self.myView addSubview:self.firstView];
self.secondView = [[UIView alloc] init];
self.secondView.frame = self.myView.bounds;
self.secondView.backgroundColor = [UIColor yellowColor];//不添加在myView上
*/
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
#pragma mark UIView动画 ---属性动画
/*
[UIView beginAnimations:@"属性动画" context:nil];
[UIView setAnimationDuration:0.1];
// 颜色的变换会有渐变的效果
self.myView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
// 大小的变化 会有一个大小的过度
// self.myView.frame = CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width * 1.2, self.myView.frame.size.height * 1.2);
// 抖动
CGPoint p = self.view.center;
self.view.frame = CGRectMake(self.view.frame.origin.x + 20, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = CGRectMake(self.view.frame.origin.x + 20, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[UIView setAnimationRepeatCount:10];//动画重复的次数
self.view.center = p;
[UIView commitAnimations];//提交动画
// [UIView beginAnimations:@"1" context:nil];
// [UIView setAnimationRepeatCount:99.5];
// self.view.center = p;
// [UIView commitAnimations];
// 设置是否有路径回退效果
// [UIView setAnimationRepeatAutoreverses:YES];
// 设置重复次数
// [UIView setAnimationRepeatCount:99.5];
// 位置的变换会有一个位置的平移效果
// self.myView.center = CGPointMake(arc4random() % 275 + 50, arc4random() % 636 + 50);
// [UIView commitAnimations];
*/
//第二种
/* [UIView animateWithDuration:1 animations:^{
self.myView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}];
*/
#pragma mark UIView动画 ---转场动画
/*
if (!_isAnimating) {
_isAnimating = YES;//标记正字啊进行动画
[UIView transitionWithView:_myView duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
if (self.firstView.superview != self.myView) {
[self.secondView removeFromSuperview];
[self.myView addSubview:self.firstView];
}else{
[self.firstView removeFromSuperview];
[self.myView addSubview:self.secondView];
}
} completion:^(BOOL finished) {
_isAnimating = NO;
}];
}
*/
#pragma mark Layer
/*
Layer是图层,UIView其实是分两层的,上面一层是透明的框负责处理用户响应,下面一层是图层,负责图形的绘制
*/
#pragma mark CGAffineTransform2D 仿射变换属性
//设置动画效果
[UIView beginAnimations:@"仿射变换" context:nil];
//设置动画速度
[UIView setAnimationDuration:1];
//旋转
/* //self.myView.transform = CGAffineTransformMakeRotation(M_PI_2);
//旋转无限次
// CGAffineTransform transform = self.myView.transform;
// self.myView.transform = CGAffineTransformRotate(transform, M_PI_2 * 10);
*/
//缩放
/*
// self.myView.transform = CGAffineTransformMakeScale(2, 1);
//多次缩放
CGAffineTransform transform = self.myView.transform;
self.myView.transform = CGAffineTransformScale(transform, 1, 1.1);
*/
//提交动画
[UIView commitAnimations];
#pragma mark CAAnimation
//1.CABaseAnimation:进行layer层的旋转
/*
CABasicAnimation *base = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];//后面的字符串是固定的名字,读取系统的文件信息
//持续时间
base.duration = 50;
//起始的角度
base.fromValue = @(0);
//终止的角度
base.toValue = @(M_PI_2 * 10000);
//将这个动画添加到layer上
[self.myView.layer addAnimation:base forKey:@"base"];
*/
//2.CAKeyFrameAnimation 用于颜色改变的
/*
CAKeyframeAnimation *key1 = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
//使用
key1.duration =5;
id c1 = (id)[UIColor greenColor].CGColor;
id c2 = (id)[UIColor blackColor].CGColor;
id c3 = (id)[UIColor yellowColor].CGColor;
id c4 = (id)[UIColor redColor].CGColor;
key1.values = @[c1,c2,c3,c4];
//添加动画
[self.myView.layer addAnimation:key1 forKey:@"color"];
self.myView.backgroundColor = [UIColor blackColor];
*/
//3.位置的改变
/*
CAKeyframeAnimation *key2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
key2.duration = 5;
NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 100, 0)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 100, [UIScreen mainScreen].bounds.size.height - 100)];
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0 , [UIScreen mainScreen].bounds.size.height - 100)];
key2.values = @[v1,v2,v3,v4,v1];
// [self.myView.layer addAnimation:key2 forKey:@"position"];
*/
//动画组
/*
同时将多个动画分组,按组添加,
*/
/*
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 5;
group.animations = @[key2,key1];
[self.myView.layer addAnimation:group forKey:@"group"];
*/
//CATransition(切换特效)
CATransition *transition = [CATransition animation];
transition.duration = 1;//设置时间
transition.type = @"pageUnCurl";//设置切换的类型
transition.subtype = kCATransitionFromLeft;//设置切换的子类型
[self.myView.layer addAnimation:transition forKey:@"transition"];
}
@end
@interface ViewController ()
@property (nonatomic,strong)UIView *myView;
//转场动画 切换的视图
@property (nonatomic,strong)UIView *firstView;
@property (nonatomic,strong)UIView *secondView;
//转场动画 标记是否正在进行动画
@property (nonatomic,assign)BOOL isAnimating;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
动画:1.UIView自带动画效果
2.Layer层动画
3.CGAffineTransform2D变换
4.核心动画
*/
self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.myView.backgroundColor = [UIColor cyanColor];
//圆角
// self.myView.layer.cornerRadius =50;
// self.myView.layer.masksToBounds = YES;
[self.view addSubview:self.myView];
//CAAnimation
// self.myView.layer.position =CGPointMake(0, 0);
// self.myView.layer.anchorPoint = CGPointMake(0,0);
// Layer 图层
/* CALayer *layer = [[CALayer alloc] init];
//
layer.frame = CGRectMake(150, 300, 80, 80);
layer.backgroundColor = [UIColor yellowColor].CGColor;
//layer的属性
//layer.masksToBounds = YES;
layer.cornerRadius = 40;
//阴影
layer.shadowColor = [UIColor redColor].CGColor;
//偏移量
layer.shadowOffset = CGSizeMake(10, 10);
//透明程度
layer.shadowOpacity = 1;
//雨化效果
layer.shadowRadius = 30;
//锚点和位置
layer.position = CGPointMake(100, 200);//位置,将锚点对应的指定的点上
layer.anchorPoint = CGPointMake(0, 0);//锚点,基准点,旋转和缩放以该点为基准点
[self.view.layer addSublayer:layer];;
*/
//转场动画 初始化两个视图
/* self.firstView = [[UIView alloc] init];
self.firstView.frame = self.myView.bounds;
self.firstView.backgroundColor = [UIColor blueColor];
[self.myView addSubview:self.firstView];
self.secondView = [[UIView alloc] init];
self.secondView.frame = self.myView.bounds;
self.secondView.backgroundColor = [UIColor yellowColor];//不添加在myView上
*/
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
#pragma mark UIView动画 ---属性动画
/*
[UIView beginAnimations:@"属性动画" context:nil];
[UIView setAnimationDuration:0.1];
// 颜色的变换会有渐变的效果
self.myView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
// 大小的变化 会有一个大小的过度
// self.myView.frame = CGRectMake(self.myView.frame.origin.x, self.myView.frame.origin.y, self.myView.frame.size.width * 1.2, self.myView.frame.size.height * 1.2);
// 抖动
CGPoint p = self.view.center;
self.view.frame = CGRectMake(self.view.frame.origin.x + 20, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = CGRectMake(self.view.frame.origin.x + 20, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[UIView setAnimationRepeatCount:10];//动画重复的次数
self.view.center = p;
[UIView commitAnimations];//提交动画
// [UIView beginAnimations:@"1" context:nil];
// [UIView setAnimationRepeatCount:99.5];
// self.view.center = p;
// [UIView commitAnimations];
// 设置是否有路径回退效果
// [UIView setAnimationRepeatAutoreverses:YES];
// 设置重复次数
// [UIView setAnimationRepeatCount:99.5];
// 位置的变换会有一个位置的平移效果
// self.myView.center = CGPointMake(arc4random() % 275 + 50, arc4random() % 636 + 50);
// [UIView commitAnimations];
*/
//第二种
/* [UIView animateWithDuration:1 animations:^{
self.myView.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}];
*/
#pragma mark UIView动画 ---转场动画
/*
if (!_isAnimating) {
_isAnimating = YES;//标记正字啊进行动画
[UIView transitionWithView:_myView duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
if (self.firstView.superview != self.myView) {
[self.secondView removeFromSuperview];
[self.myView addSubview:self.firstView];
}else{
[self.firstView removeFromSuperview];
[self.myView addSubview:self.secondView];
}
} completion:^(BOOL finished) {
_isAnimating = NO;
}];
}
*/
#pragma mark Layer
/*
Layer是图层,UIView其实是分两层的,上面一层是透明的框负责处理用户响应,下面一层是图层,负责图形的绘制
*/
#pragma mark CGAffineTransform2D 仿射变换属性
//设置动画效果
[UIView beginAnimations:@"仿射变换" context:nil];
//设置动画速度
[UIView setAnimationDuration:1];
//旋转
/* //self.myView.transform = CGAffineTransformMakeRotation(M_PI_2);
//旋转无限次
// CGAffineTransform transform = self.myView.transform;
// self.myView.transform = CGAffineTransformRotate(transform, M_PI_2 * 10);
*/
//缩放
/*
// self.myView.transform = CGAffineTransformMakeScale(2, 1);
//多次缩放
CGAffineTransform transform = self.myView.transform;
self.myView.transform = CGAffineTransformScale(transform, 1, 1.1);
*/
//提交动画
[UIView commitAnimations];
#pragma mark CAAnimation
//1.CABaseAnimation:进行layer层的旋转
/*
CABasicAnimation *base = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];//后面的字符串是固定的名字,读取系统的文件信息
//持续时间
base.duration = 50;
//起始的角度
base.fromValue = @(0);
//终止的角度
base.toValue = @(M_PI_2 * 10000);
//将这个动画添加到layer上
[self.myView.layer addAnimation:base forKey:@"base"];
*/
//2.CAKeyFrameAnimation 用于颜色改变的
/*
CAKeyframeAnimation *key1 = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
//使用
key1.duration =5;
id c1 = (id)[UIColor greenColor].CGColor;
id c2 = (id)[UIColor blackColor].CGColor;
id c3 = (id)[UIColor yellowColor].CGColor;
id c4 = (id)[UIColor redColor].CGColor;
key1.values = @[c1,c2,c3,c4];
//添加动画
[self.myView.layer addAnimation:key1 forKey:@"color"];
self.myView.backgroundColor = [UIColor blackColor];
*/
//3.位置的改变
/*
CAKeyframeAnimation *key2 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
key2.duration = 5;
NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 100, 0)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake([UIScreen mainScreen].bounds.size.width - 100, [UIScreen mainScreen].bounds.size.height - 100)];
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0 , [UIScreen mainScreen].bounds.size.height - 100)];
key2.values = @[v1,v2,v3,v4,v1];
// [self.myView.layer addAnimation:key2 forKey:@"position"];
*/
//动画组
/*
同时将多个动画分组,按组添加,
*/
/*
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = 5;
group.animations = @[key2,key1];
[self.myView.layer addAnimation:group forKey:@"group"];
*/
//CATransition(切换特效)
CATransition *transition = [CATransition animation];
transition.duration = 1;//设置时间
transition.type = @"pageUnCurl";//设置切换的类型
transition.subtype = kCATransitionFromLeft;//设置切换的子类型
[self.myView.layer addAnimation:transition forKey:@"transition"];
}
@end
相关文章推荐
- String baseprice = request.getParameter("baseprice")==null相关问题
- UE4 动画系统
- UE4 动画系统
- UIPopoverController
- iOS UI-团购案例(通过xib文件自定义UITableViewCell)
- iOS开发UI篇—懒加载
- UE4 创建第三人称角色
- UE4 创建第三人称角色
- Upgrade bank is empty or corrupted for FPC 0, please do standard upgrade sequence 错误解决&juniper 交换机系统升级
- java/scala优先队列(PriorityQueue)元素改变后如何实现有序
- 在iOS 8中使用UIAlertController
- iOS UI学习笔记(六)UIViewController
- uuid
- 在php环境中使用ueditor富文本编辑器步骤详解
- UISearchBar协议常用方法
- 带回收功能的lockfree queue的java实现
- [leetcode] 304. Range Sum Query 2D - Immutable
- Storyboard里面的几种Segue区别及视图的切换:push,modal,popover,replace和custom
- iOS-UIImageView对象
- PhotosUI(PHLivePhotoView)