如何顺序应用不同视图的动画?

时间:2013-08-06 11:25:05

标签: ios core-animation

我需要应用动画的四个视图。 如果用户点击按钮,动画是view1应该隐藏1秒,然后是view2,然后是view3,然后是view4

问题是如何在序列中的不同视图中应用动画?

我需要使用Core Animation来完成它。

3 个答案:

答案 0 :(得分:2)

使用animateWithDuration:animations:completion:方法,其中完成块调用运行下一个动画的方法(类似地,它的完成块启动链的下一部分)。

答案 1 :(得分:0)

嵌套动画可以帮助您实现所需目标。

参考Animations in UIView

- (IBAction)yoiurButtonClickAction:(id)sender{
    [UIView animateWithDuration:1.0
                          delay: 0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         _view1.alpha = 0.0;
                     }
                     completion:^(BOOL finished){
                         [UIView animateWithDuration:1.0
                                               delay: 0.0
                                             options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              _view1.alpha = 1.0;
                                              _view2.alpha = 0.0;
                                          }
                                          completion:^(BOOL finished){
                                              [UIView animateWithDuration:1.0
                                                                    delay: 0.0
                                                                  options:UIViewAnimationOptionCurveEaseInOut
                                                               animations:^{
                                                                   _view2.alpha = 1.0;
                                                                   _view3.alpha = 0.0;
                                                               }
                                                               completion:^(BOOL finished){
                                                                   [UIView animateWithDuration:1.0
                                                                                         delay: 0.0
                                                                                       options:UIViewAnimationOptionCurveEaseInOut
                                                                                    animations:^{
                                                                                        _view3.alpha = 1.0;
                                                                                        _view4.alpha = 0.0;
                                                                                    }
                                                                                    completion:^(BOOL finished){
                                                                                        [UIView animateWithDuration:1.0
                                                                                                              delay: 0.0
                                                                                                            options:UIViewAnimationOptionCurveEaseInOut
                                                                                                         animations:^{
                                                                                                             _view4.alpha = 1.0;

                                                                                                         }
                                                                                                         completion:^(BOOL finished){

                                                                                                         }];
                                                                                    }];
                                                               }];
                                          }];
                     }];
}

答案 2 :(得分:0)

试试这段代码,

小时。

NSInteger currentView;
UIView *view1, *view2, *view3, *view4;

米。

- (无效)viewDidLoad中 {

[super viewDidLoad];

currentView = 1;

view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 100)];
[view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view1];

view2 = [[UIView alloc]initWithFrame:CGRectMake(80, 0, 80, 100)];
[view2 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:view2];

view3 = [[UIView alloc]initWithFrame:CGRectMake(160, 0, 80, 100)];
[view3 setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:view3];

view4 = [[UIView alloc]initWithFrame:CGRectMake(240, 0, 80, 100)];
[view4 setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:view4];

}

- (IBAction为)buttonPressed:(ID)发送方 {

[self runAnimation];    

}

- (UIView *)viewForAnimation:(NSInteger)索引 {

switch (index)
{
    case 1:
        return view1;
    case 2:
        return view2;
    case 3:
        return view3;
    case 4:
        return view4;
}
return view1;

}

- (无效)runAnimation {

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear
                 animations:^
 {
     [self viewForAnimation:currentView].hidden = YES;
 }
                 completion:^(BOOL finished)
 {
     currentView++;
     if(currentView < 5)
         [self performSelector:@selector(runAnimation) withObject:nil afterDelay:1.5];
 }
 ];

}