UI:动画
2015-10-08 10:40
441 查看
参考
UIView 层级管理、触摸、检测手机是否横屏、调整设备的方向
动画:为了提高用户的体验
View层的动画、UIlayer层的动画。UIView改变视图效果、UIlayer的绘图框架
#pragma mark-IOS 4之前动画操作
//开始动画
- (IBAction)starAnimation:(id)sender {
//UIview 的一些属性的变化,来实现动画效果
[self handlePropertyAnimation];
}
#pragma mark---UIView Animation---
-(void)handlePropertyAnimation{
//通过改变 UIView 属性(frame bounds center alpha transform旋转 )来达到一些动画
//参数1 参数2
//IOS4之前的动画方式,必须写在动画开始 与 结束之间
[UIView beginAnimations:nil context:nil];//开始动画
//设置动画时长 参数:动画效果类型
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画的变化曲线
[UIView setAnimationDuration:5];
//动画是否需要延迟
[UIView setAnimationDelay:0.5];
//设置动画的重复次数
[UIView setAnimationRepeatCount:5];
//改变视图的中心点位置
CGPoint center = self.animationView.center;
center.y += 80;
self.animationView.center = center;
//改变alpha 值
self.animationView.alpha = 0.2;
//动画是否从当前的状态开始
[UIView setAnimationBeginsFromCurrentState:YES];
//是否翻转动画
[UIView setAnimationRepeatAutoreverses:YES];
//翻转参数设置
self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, 0.5);
//旋转的时候改变自身的缩放比
self.animationView.transform = CGAffineTransformScale(self.animationView.transform, 0.5, 0.5);
//改变视图的背景颜色
self.animationView.backgroundColor =[UIColor yellowColor];
//设置动画的代理(这个不用遵循协议)
[UIView setAnimationDelegate:self];
//动画结束的时候出发的方法
[UIView setAnimationDidStopSelector:@selector(animationStop)];
//动画开始的时候触发的方法
[UIView setAnimationWillStartSelector:@selector(animationWillStart)];
[UIView commitAnimations];//结束动画
}
#pragma mark---动画开始与结束的时候触发方法
-(void)animationStop{
NSLog(@"动画结束");
//相应的操作
}
-(void)animationWillStart{
NSLog(@"动画开始");
//相应的操作
}
#pragma mark-IOS 4之后动画操作
-(void)handleBlockAnimation{
//参数1: 动画持续时间 参数2: 动画的属性
[UIView animateWithDuration:5 animations:^{
CGPoint center = self.animationView.center;
center.y += 80;
self.animationView.center = center;
}];
//中级用法 参数1 动画持续时间 参数2 动画属性 参数3 动画之后的效果
[UIView animateWithDuration:5 animations:^{
self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, 0.5);
self.animationView.bounds = CGRectMake(0, 0, 50, 50);
self.animationView.transform = CGAffineTransformMakeTranslation(10, 12);
} completion:^(BOOL finished) {
self.animationView.transform = CGAffineTransformMakeScale(0.4, 0.2);
[self animationStop];
}];
//高级用法 参数1 动画持续时间 参数2 延迟时间 参数3 动画开始与结束后的操作
[UIView animateWithDuration:1 delay:0.2 options:UIViewAnimationOptionAllowUserInteraction animations:^{
CGPoint center = self.animationView.center;
center.x +=100;
self.animationView.center = center;
self.animationView.alpha = 0.1;
} completion:^(BOOL finished) {
[self animationStop];
}];
//弹簧效果 参数1 动画持续时间 参数2 延迟时间 参数3:弹簧强度,范围最大值是1 参数4:开始速度 参数5:动画速度 参数6:动画效果 参数7:结束时候的操作
[UIView animateWithDuration:0.5 delay:0.2 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:UIViewAnimationOptionLayoutSubviews animations:^{
CGPoint center = self.bounTf.center;
center.y += 50;
self.bounTf.center = center;
self.bounTf.transform = CGAffineTransformMakeScale(1, 1.2);
//发抖的效果
[self.bounTf shake];
} completion:^(BOOL finished) {
}];
}
View Code UILayer 在图层创建的时候创键的动画方法
UIView 层级管理、触摸、检测手机是否横屏、调整设备的方向
动画:为了提高用户的体验
View层的动画、UIlayer层的动画。UIView改变视图效果、UIlayer的绘图框架
#pragma mark-IOS 4之前动画操作
//开始动画
- (IBAction)starAnimation:(id)sender {
//UIview 的一些属性的变化,来实现动画效果
[self handlePropertyAnimation];
}
#pragma mark---UIView Animation---
-(void)handlePropertyAnimation{
//通过改变 UIView 属性(frame bounds center alpha transform旋转 )来达到一些动画
//参数1 参数2
//IOS4之前的动画方式,必须写在动画开始 与 结束之间
[UIView beginAnimations:nil context:nil];//开始动画
//设置动画时长 参数:动画效果类型
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画的变化曲线
[UIView setAnimationDuration:5];
//动画是否需要延迟
[UIView setAnimationDelay:0.5];
//设置动画的重复次数
[UIView setAnimationRepeatCount:5];
//改变视图的中心点位置
CGPoint center = self.animationView.center;
center.y += 80;
self.animationView.center = center;
//改变alpha 值
self.animationView.alpha = 0.2;
//动画是否从当前的状态开始
[UIView setAnimationBeginsFromCurrentState:YES];
//是否翻转动画
[UIView setAnimationRepeatAutoreverses:YES];
//翻转参数设置
self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, 0.5);
//旋转的时候改变自身的缩放比
self.animationView.transform = CGAffineTransformScale(self.animationView.transform, 0.5, 0.5);
//改变视图的背景颜色
self.animationView.backgroundColor =[UIColor yellowColor];
//设置动画的代理(这个不用遵循协议)
[UIView setAnimationDelegate:self];
//动画结束的时候出发的方法
[UIView setAnimationDidStopSelector:@selector(animationStop)];
//动画开始的时候触发的方法
[UIView setAnimationWillStartSelector:@selector(animationWillStart)];
[UIView commitAnimations];//结束动画
}
#pragma mark---动画开始与结束的时候触发方法
-(void)animationStop{
NSLog(@"动画结束");
//相应的操作
}
-(void)animationWillStart{
NSLog(@"动画开始");
//相应的操作
}
#pragma mark-IOS 4之后动画操作
-(void)handleBlockAnimation{
//参数1: 动画持续时间 参数2: 动画的属性
[UIView animateWithDuration:5 animations:^{
CGPoint center = self.animationView.center;
center.y += 80;
self.animationView.center = center;
}];
//中级用法 参数1 动画持续时间 参数2 动画属性 参数3 动画之后的效果
[UIView animateWithDuration:5 animations:^{
self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, 0.5);
self.animationView.bounds = CGRectMake(0, 0, 50, 50);
self.animationView.transform = CGAffineTransformMakeTranslation(10, 12);
} completion:^(BOOL finished) {
self.animationView.transform = CGAffineTransformMakeScale(0.4, 0.2);
[self animationStop];
}];
//高级用法 参数1 动画持续时间 参数2 延迟时间 参数3 动画开始与结束后的操作
[UIView animateWithDuration:1 delay:0.2 options:UIViewAnimationOptionAllowUserInteraction animations:^{
CGPoint center = self.animationView.center;
center.x +=100;
self.animationView.center = center;
self.animationView.alpha = 0.1;
} completion:^(BOOL finished) {
[self animationStop];
}];
//弹簧效果 参数1 动画持续时间 参数2 延迟时间 参数3:弹簧强度,范围最大值是1 参数4:开始速度 参数5:动画速度 参数6:动画效果 参数7:结束时候的操作
[UIView animateWithDuration:0.5 delay:0.2 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:UIViewAnimationOptionLayoutSubviews animations:^{
CGPoint center = self.bounTf.center;
center.y += 50;
self.bounTf.center = center;
self.bounTf.transform = CGAffineTransformMakeScale(1, 1.2);
//发抖的效果
[self.bounTf shake];
} completion:^(BOOL finished) {
}];
}
//代码: #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *image; @property (weak, nonatomic) IBOutlet UITextField *boundsTf; @property (weak, nonatomic) IBOutlet UIImageView *ballon; @end /* UIView 的四个动画样式 UIlayer 的四个动画样式 */ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)but1:(id)sender { // CABaseAnimation CABasicAnimation * baseAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"]; //开始值 baseAnimation.fromValue = @(0);//需一个对象类型 //结束值 baseAnimation.toValue = @(400); baseAnimation.duration = 5.0;//动画持续时长单位秒 baseAnimation.repeatCount = 2;//动画持续次数 //添加动画到视图的 layer上 [self.image.layer addAnimation:baseAnimation forKey:nil];//需要添加一个标志 } - (IBAction)but2:(id)sender { //使用关键帧动画 CAKeyframeAnimation * keyframe = [CAKeyframeAnimation animationWithKeyPath:@"position"]; CGPoint po1 = self.image.center; CGPoint po2 = CGPointMake(200, 400); CGPoint po3 = CGPointMake(400, 100); NSValue * value1 = [NSValue valueWithCGPoint:po1 ];//将其他类型转化为对应的 Value NSValue * value2 = [NSValue valueWithCGPoint:po2]; NSValue * value3 = [NSValue valueWithCGPoint:po3]; keyframe.values = @[value1,value2,value3,value1];//存储路径中变化的多个值 keyframe.duration = 6;//持续时间 keyframe.repeatCount = 2; [self.image.layer addAnimation:keyframe forKey:nil]; } - (IBAction)but3:(id)sender { //动画元素01 CAKeyframeAnimation *keyframe1 = [CAKeyframeAnimation animationWithKeyPath:@"position"]; //贝塞尔曲线 //参数1 绕转的中心点坐标 参数2:圆角设为直径的一半 参数3:开始角度 参数4:结束角度 参数5:环绕的方向 YES就是按照时钟的方向 NO就是时钟的反方向转动 //沿着圆形移动 CGPoint po1 =CGPointMake(0, self.view.frame.size.height/2); UIBezierPath * bezierPath = [UIBezierPath bezierPathWithArcCenter:po1 radius:self.view.frame.size.height/2 startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES]; keyframe1.repeatCount = 5; //以贝塞尔曲线作为移动的路径 keyframe1.path = bezierPath.CGPath; //动画元素02 //让气球缩放效果 CAKeyframeAnimation * keyframe2 = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"]; keyframe2.values = @[@(1.0),@(1.2),@(1.5),@(1.8),@(8),@(1.2),@(1.0),@(0.8),@(0.6),]; keyframe2.repeatCount = 2; //创建动画分组 CAAnimationGroup * groupAnimation = [CAAnimationGroup animation]; groupAnimation.animations = @[keyframe1,keyframe2]; groupAnimation.duration = 10;//持续时间 groupAnimation.repeatCount = 20;//持续次数 //将动画添加到 layer 层 [self.ballon.layer addAnimation:groupAnimation forKey:nil]; } //CATransition 动画 CALayer 层的过度动画 - (IBAction)but4:(id)sender { //这对layer 切换效果,可以设置切换动画的样式 CATransition * transition = [CATransition animation]; transition.type = kCATransitionPush; /*类型有: cube立方体 pageCurl翻页的类型 下面的类型是文档中的,上面的是OC的字符串,他也能识别 kCATransitionFade ; kCATransitionMoveIn ; kCATransitionPush ; kCATransitionReveal; */ transition.subtype = kCATransitionFromLeft;//给一个切换的方向 transition.duration = 1;//持续时间 transition.repeatCount = 2; [self.view.layer addAnimation:transition forKey:nil];//添加到要是实现的动画上 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
View Code UILayer 在图层创建的时候创键的动画方法
相关文章推荐
- 给UIview加上xib
- iOS开发之UITabBarController
- iOS-UITableviewCell的重用机制
- iOS开发工具Xcode:Interface Builder
- Mniui 学习笔记 2
- UI Testing in Xcode 7
- marguee实现最新公告动态轮播效果
- LeetCode 51 N-Queens
- UItableViewCell 编辑移动
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
- jsp ueditor 图片在线管理图片路径
- Exported service does not require permission警告
- iOS的UIColor类与其相关类之间的区别及判断相等的方法
- Block高级用法:Block传值UI_12(3)
- UITabBarController及三种控制器的…
- 导航控制器 UI_08(上)
- UIPassValue页面传值 UI_08(下)
- 实战项目:通讯录 UI—第十一天
- UITableViewBase UI_09
- TableEdit UI_10