UIView动画

9 篇文章 0 订阅
2 篇文章 0 订阅

第一、综述

UIView类的很多属性都设计为动画,动画的属性是指当属性从一个值变成另外一个值时可以支持动画,同时通知UIKit需要执行什么样类型的动画,
UIView对象支持动画的属性有如下几个:
frame属性:可以使用该属性改变尺寸和位置
bounds:改变尺寸
center:改变视图的位置
alpha:改变视图的透明度
backgroundColor:改变视图的背景
contentStretch:改变视图内容如何拉伸
Both UIKit and Core Animation provide support for animations, but the level of support provided by each technology varies. In UIKit, animations are performed using UIView objects.


第二、常见方法解析

+ (void)setAnimationDelegate:(id)delegate     设置动画代理对象,当动画开始或者结束时会发消息给代理对象
+ (void)setAnimationWillStartSelector:(SEL)selector   当动画即将开始时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDidStopSelector:(SEL)selector  当动画结束时,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selector
+ (void)setAnimationDuration:(NSTimeInterval)duration   动画的持续时间,秒为单位
+ (void)setAnimationDelay:(NSTimeInterval)delay  动画延迟delay秒后再开始
+ (void)setAnimationStartDate:(NSDate *)startDate   动画的开始时间,默认为now
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve  动画的节奏控制
+ (void)setAnimationRepeatCount:(float)repeatCount  动画的重复次数
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses  如果设置为YES,代表动画每次重复执行的效果会跟上一次相反

+ (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache  设置视图view的过渡效果, transition指定过渡类型, cache设置YES代表使用视图缓存,性能较好


第三、翻转的动画
//开始动画
[UIView beginAnimations:@"wap view" context:nil];
//设置时常
[UIView setAnimationDuration:1];
//设置动画淡入淡出
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//设置代理
[UIView setAnimationDelegate:self];
//设置翻转方向
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromLeft forView:manImageView cache:YES];
//动画结束

[UIView commitAnimations];


第四、旋转动画
UIView有个transform的属性,通过设置该属性,我们可以实现调整该view在其superView中的大小和位置,具体来说,Transform(变化矩阵)是一种3×3的矩阵,通过这个矩阵我们可以对一个坐标系统进行缩放,平移,旋转以及这两者的任意组着操作。而且矩阵的操作不具备交换律,即矩阵的操作的顺序不同会导致不同的结果。
获得CGAffineTransform有多种方法,例如使用CGAffineTransformMake,但是对于矩阵操作相对比较麻烦,但是事实上iOS已经为我们提供好了三个对应的方法,分别用于在原来的角度、缩放、移动位置的基础上做出修改:CGAffineTransformRotate、CGAffineTransformScale、CGAffineTransformTranslate
 //  实现的是放大和缩小
    view.transform=CGAffineTransformScale(view.transform, 0.5, 0.5);  
  //实现的是旋转
    view.transform=CGAffineTransformRotate(view.transform, M_PI);
//实现的是平移
    view.transform=CGAffineTransformTranslate(view.transform,20, 20);

创建一个CGAffineTransform transform对象
CGAffineTransform transform;
//设置旋转度数
transform = CGAffineTransformRotate(manImageView.transform,M_PI/6.0);
//动画开始
[UIView beginAnimations:@"rotate" context:nil ];
//动画时常
[UIView setAnimationDuration:2];
//添加代理
[UIView setAnimationDelegate:self];
//获取transform的值
[manImageView setTransform:transform];
//关闭动画

[UIView commitAnimations];


第五、缩放动画
CGAffineTransform transform;
transform = CGAffineTransformScale(manImageView.transform,1.2,1.2);
[UIView beginAnimations:@"scale" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
[manImageView setTransform:transform];
[UIView commitAnimations];
取反的动画效果是根据当前的动画取他的相反的动画
CGAffineTransform transform;
transform=CGAffineTransformInvert(manImageView.transform);
[UIView beginAnimations:@"Invert" context:nil];
[UIView setAnimationDuration:2];//动画时常
[UIView setAnimationDelegate:self];
[manImageView setTransform:transform];//获取改变后的view的transform

[UIView commitAnimations];//关闭动画


第六、偏移动画
[UIView beginAnimations:@"move" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
//改变它的frame的x,y的值
manImageView.frame=CGRectMake(100,100, 120,100);

[UIView commitAnimations];


第七、翻页动画
[UIView beginAnimations:@"curlUp" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//指定动画曲线类型,该枚举是默认的,线性的是匀速的
//设置动画时常
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
//设置翻页的方向
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:manImageView cache:YES];
//关闭动画

[UIView commitAnimations];


第八、UIImageView的帧动画

UIImageView可以让一系列的图片在特定的时间内按顺序显示
相关属性解析:
animationImages:要显示的图片(一个装着UIImage的NSArray)
animationDuration:完整地显示一次animationImages中的所有图片所需的时间
animationRepeatCount:动画的执行次数(默认为0,代表无限循环)
相关方法解析:
- (void)startAnimating; 开始动画
- (void)stopAnimating;  停止动画
- (BOOL)isAnimating;  是否正在运行动画

第九、UIActivityIndicatorView

是一个旋转进度轮,可以用来告知用户有一个操作正在进行中,一般用initWithActivityIndicatorStyle初始化
方法解析:
- (void)startAnimating; 开始动画
- (void)stopAnimating;  停止动画
- (BOOL)isAnimating;  是否正在运行动画
UIActivityIndicatorViewStyle有3个值可供选择:
UIActivityIndicatorViewStyleWhiteLarge   //大型白色指示器    
UIActivityIndicatorViewStyleWhite      //标准尺寸白色指示器    
UIActivityIndicatorViewStyleGray    //灰色指示器,用于白色背景

第十、block实现

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
参数解析:
duration:动画的持续时间
delay:动画延迟delay秒后开始
options:动画的节奏控制
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block

1、[UIView animateWithDuration:2 animations:^{

        //subView.transform=CGAffineTransformTranslate(subView.transform,20, 20);
        //subView.frame=CGRectMake(30, 30, 100, 300);
        //subView.center=CGPointMake(100, 100);
        subView.backgroundColor=[UIColor blueColor];
        //subView.alpha=0.5;
    } completion:^(BOOL finished) {
    }];

2、利用block来实现动画,回调简单方便
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame=self.myView.frame;
           frame.origin.y=300;
            self.myView.frame=frame;
        //self.myView.alpha=0.0;
    } completion:^(BOOL finished)
     {
         [UIView animateWithDuration:0.5 animations:^{
             //self.myView.alpha=1.0;
             CGRect frame=self.myView.frame;
             frame.origin.y=10;
             self.myView.frame=frame;
         }];
     }];
在completion后面的实现前面动画结束后的调用的方法
3、实现渐入渐出得效果
    [UIView animateWithDuration:0.5 animations:^{
        self.myView.alpha = 0;
    } completion:^(BOOL finished)
     {
         self.myView.alpha = 1;
     }];
4、实现放大缩小
    self.myView.transform=CGAffineTransformScale(self.myView.transform, 1, 1);
    [UIView animateWithDuration:0.5 animations:^{
        self.myView.transform=CGAffineTransformScale(self.myView.transform, 0.1, 0.1);
    } completion:^(BOOL finished)
     {
         [UIView animateWithDuration:0.5 animations:^{
             self.myView.transform=CGAffineTransformIdentity;
         }];

     }]; 

第十一、两个动画间的过渡动画

上面讲的动画是针对一个动画的旋转、平移、放大缩小,我们分析两个动画在切换时的动画效果

 
 
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.parentView cache:YES];
[UIView commitAnimations];
[self.parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

第十二、修改pushviewcontroller切换动画效果

   [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:YES];
    [UIView commitAnimations];
    UIViewController *viewContr = [[UIViewController alloc] init];
   [self.navigationController pushViewController:viewContr animated:NO];

第十三、改变一个view内部子view的动画
+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
参数解析:
duration:动画的持续时间
view:需要进行转场动画的视图
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block

 [UIView transitionWithView:self.view duration:2 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{

        _view2.hidden=YES;

    } completion:^(BOOL finished) {

        _view1.hidden=NO;

        _view2.hidden=NO;

    }];

第十四、替换不同的view的动画

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
方法调用完毕后,相当于执行了下面两句代码:
// 添加toView到父视图
[fromView.superview addSubview:toView]; 
// 把fromView从父视图中移除
[fromView.superview removeFromSuperview];
参数解析:
duration:动画的持续时间
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block  

   [UIView transitionFromView:_view1 toView:_view2 duration:2options:UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) {

    }];



转载:http://blog.csdn.net/richard_rufeng/article/details/9981303


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值