动画学习之Animating Views with Blocks

有五个函数
 ios 4.0 以后才支持
Animating Views with Blocks
+ animateWithDuration:delay:options:animations:completion:
+ animateWithDuration:animations:completion:
+ animateWithDuration:animations:
+ transitionWithView:duration:options:animations:completion:
+ transitionFromView:toView:duration:options:completion:

这三个函数,用来支持多个不同的view(view1,view2。。。。。。)的动画
Animate changes to one or more views
+ animateWithDuration:delay:options:animations:completion:
+ animateWithDuration:animations:completion:
+ animateWithDuration:animations:

这个函数,用来对一个容器view的一个动画进行操作,这个容器内的所有subview将一起动画
+ transitionWithView:duration:options:animations:completion:

这个函数,用来对一个容器view的多个subview进行动画,容器view中的FromView到toView的切换等动画
比如:subview1翻转后显示subview2
+ transitionFromView:toView:duration:options:completion:
/* 翻转示例
     这个方法完成firstView翻转过来显示背面的secondview,并且将firstview移除。
     需要注意的事情是,我们在翻转的时候要给firstView和secondview增加一个共同的容器view
     将这两个view作为一个容器view的subview,才能正确执行,否则系统将会默认将self.view作为容器view,导致效果不正确。
     
     在viewdidload中增加一下代码:
     - (void)viedDidLoad{
        [self.secondView removeFromSuperview];
        UIView *containerView = [[UIView alloc] initWithFrame:self.firstView.frame];
        self.firstView.frame = containerView.bounds;
        self.secondView.frame = containerView.bounds;
        [containerView addSubview:self.secondView];
        [containerView addSubview:self.firstView];
        [self.view addSubview:containerView];
     
     [super viewDidLoad];
     }
     
     - (void)animation {
        [UIView transitionFromView:self.firstView toView:self.secondView duration:2.f options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) {
        }];
     }
*/
     


options的选项:
看下面代码中的注释:

- (NSInteger)animationOption: (NSInteger)index {  
    NSInteger type = 0;  
    switch (index) {  
        case 0: {// 跟父类作为一个整体一起动画,此方式为默认方式  
            type = UIViewAnimationOptionLayoutSubviews;  
            break;  
        }  
        case 1: {// 在动画运行过程中,允许用户与之交互操作  
            /* 
             注意1,UIViewAnimationOptionAllowUserInteraction只是说允许用户点击,但在动画过程中, 
             比如一个让btnOne从(0,0)移动到(100,0)的动画,在动画播放的过程中其实btnOne的坐标已经是(100,0)了 你点击在view上显示的btn是没办法获取响应的,你需要在(100,0)处进行点击才能让btnOne响应你的点击事件。 
             解决这个问题的一种方法就是用定时器不断刷,把动画微分化: 
             - (void)viewDidLoad 
             { 
                _btnMove = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
                [_btnMove addTarget:self action:@selector(btnMoveDidSelect) forControlEvents:UIControlEventTouchUpInside]; 
                _btnMove.frame = CGRectMake(0, 0, 80, 40); 
                [_btnMove setTitle:@"touch me!" forState:UIControlStateNormal]; 
                [self.view addSubview:_btnMove]; 
                [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(buttonMoved) userInfo:nil repeats:YES]; 
                [super viewDidLoad]; 
                return; 
             } 
              
             - (void)buttonMoved 
             { 
                if(_btnMove.frame.origin.x != 250) 
                { 
                    [UIView beginAnimati*****:nil context:nil]; 
                    [UIView setAnimationDuration:0.01f]; 
                    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
                    [_btnMove setFrame: CGRectMake(_btnMove.frame.origin.x+1, _btnMove.frame.origin.y,_btnMove.frame.size.width, _btnMove.frame.size.height)]; 
                    [UIView commitAnimati*****]; 
                } 
             } 
              
             注意2:如果你设置了UIViewAnimationOptionAllowUserInteraction,界面上其他的button等控件可以接受事件,但是你不设置UIViewAnimationOptionAllowUserInteraction,其他的控件事件都收不到。也就是说如果设置了UIViewAnimationOptionAllowUserInteraction,在动画期间,主线程会有时间接受事件。 
             */  
            type = UIViewAnimationOptionAllowUserInteraction;  
            break;  
        }  
        case 2: {// 从当前状态开始动画。例如如下代码  
            /* 
             - (void)doAnimation1 { 
                [UIView animateWithDuration:5.0 
                                      delay:0.0 
                                    options:nil 
                animations:^{ 
                    self.firstView.frame = CGRectMake(0, 0, 200, 200); 
                    [self performSelector:@selector(doAnimation2) withObject:nil afterDelay:1]; 
                } 
                completion:^(BOOL finished){}]; 
            } 
              
             - (void)doAnimation2 { 
                [UIView animateWithDuration:5.0 
                                      delay:0.0 
                                    options:UIViewAnimationOptionBeginFromCurrentState 
                animations:^{ 
                    self.firstView.frame = CGRectMake(660, 660, 200, 200); 
                } 
                completion:^(BOOL finished){}]; 
            } 
             当5秒的doAnimation1动画运行1秒后,开始doAnimation2动画(doAnimation2设定了UIViewAnimationOptionBeginFromCurrentState参数),则doAnimation2的动画是从doAnimation1运行到1秒的状态为开始状态继续运行。如果不传入这个参数,则doAnimation2的开始状态为doAnimation1完整运行结束后的状态。 
             */  
            type = UIViewAnimationOptionBeginFromCurrentState;  
            break;  
        }  
        case 3: {// 重复执行一个动画,从初始状态到结束状态,然后瞬间跳到初始状态继续无限次的执行同一动作  
            type = UIViewAnimationOptionRepeat;  
            break;  
        }  
        case 4: {// 反向执行一个动画。当动画执行完一遍后,沿原路径反向执行一遍。这个属性必须跟UIViewAnimationOptionRepeat一起使用  
            type = UIViewAnimationOptionAutoreverse;  
            break;  
        }  
        case 5: {// 忽略子层嵌套的动画的时间间隔。例如如下代码  
            /* 
             [UIView transitionWithView:self.firstView duration:4.f options:nil animations:^{ 
              
                    self.firstView.frame = CGRectMake(0, 0, 200, 200);  
              
                    [UIView transitionWithView:self.firstView duration:1.f options:nil animations:^{ 
              
                            self.secondView.frame = CGRectMake(0, 0, 200, 200);        
              
                    } completion:^(BOOL finished) {}]; 
             } completion:^(BOOL finished) {}]; 
             self.firstView的动画时间是4秒,self.secondView的动画时间是1秒, 
             如果子层不设置UIViewAnimationOptionOverrideInheritedDuration(如父层和子层都是nil), 
             则子层默认继承父层的时间,忽略自己的时间。 
              
             若是子层设置UIViewAnimationOptionOverrideInheritedDuration属性, 
             则子层将按照自身设置的1秒的时间执行。 
             */  
            type = UIViewAnimationOptionOverrideInheritedDuration;  
            break;  
        }  
        case 6: {// 忽略子层嵌套的动画属性的时间 (如UIViewAnimationOptionCurveEaseInOut) 同上  
            type = UIViewAnimationOptionOverrideInheritedCurve;  
            break;  
        }  
        case 7: {// 允许同一个view的多个动画同时进行  
            type = UIViewAnimationOptionAllowAnimatedContent;  
            break;  
        }  
        case 8: {// 控制两个subview的显示和隐藏  
            /* 
             控制两个subview的显示和隐藏(功能类似于addsubview和removefromsuperview),使用这个属性(一般在transitionFromView:toView:duration:options:completion:这个方法中使用),FromView会被隐藏,toView会被显示 
              
             - (void)viedDidLoad{ 
                [self.secondView removeFromSuperview]; 
                UIView *containerView = [[UIView alloc] initWithFrame:self.firstView.frame]; 
                self.firstView.frame = containerView.bounds; 
                self.secondView.frame = containerView.bounds; 
                [containerView addSubview:self.secondView]; 
                [containerView addSubview:self.firstView]; 
                [self.view addSubview:containerView]; 
              
                [super viewDidLoad]; 
             } 
              
             - (void)animation { 
                [UIView transitionFromView:self.firstView toView:self.secondView duration:2.f options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) {}]; 
             } 
              
             */  
            type = UIViewAnimationOptionShowHideTransitionViews;  
            break;  
        }  
            // ========= 动画过渡动作的速度  
        case 9: {// 先慢后快再慢  
            type = UIViewAnimationOptionCurveEaseInOut;  
            break;  
        }  
        case 10: {// 先慢后快  
            type = UIViewAnimationOptionCurveEaseIn;  
            break;  
        }  
        case 11: {// 先快后慢  
            type = UIViewAnimationOptionCurveEaseOut;  
            break;  
        }  
        case 12: {// 匀速  
            type = UIViewAnimationOptionCurveLinear;  
            break;  
        }  
            // ========= 动画过渡过程的方式:  
        case 13: {// 不指定方式  
            type = UIViewAnimationOptionTransitionNone;  
            break;  
        }  
        case 14: {// 翻转  
            type = UIViewAnimationOptionTransitionFlipFromLeft;  
            break;  
        }  
        case 15: {// 翻转  
            type = UIViewAnimationOptionTransitionFlipFromRight;  
            break;  
        }  
        case 16: {// 翻转  
            type = UIViewAnimationOptionTransitionFlipFromTop; // 5.0以后  
            break;  
        }  
        case 17: {// 翻转  
            type = UIViewAnimationOptionTransitionFlipFromBottom;// 5.0以后  
            break;  
        }  
        case 18: {// 重叠,当一个view从一个位置到另一个位置时,当前的view会由透明状态逐渐显示到目的位置,原来的位置将会保留一个影子,并逐渐消失  
            type = UIViewAnimationOptionTransitionCrossDissolve;// 5.0以后  
            break;  
        }  
        case 19: {// 翻页  
            type = UIViewAnimationOptionTransitionCurlUp;  
            break;  
        }  
        case 20: {// 翻页  
            type = UIViewAnimationOptionTransitionCurlDown;  
            break;  
        }  
        default:  
            break;  
    }  
      
    return type;  
}  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值