如何在动画后更改视图?

时间:2014-02-20 19:03:53

标签: ios animation ios7

我需要你的帮助。 在我的应用程序中,我按一个按钮,动画开始。在此动画之后,我想在不按任何其他按钮的情况下自动更改视图。 我该怎么办?

这里是我的代码,我想我的方式是正确的,但是动画没有启动,只更改了视图,xcode没有给我错误信息。

- (IBAction)button {

    [UIView animateWithDuration:2.5f animations:^{

        NSArray *playAnimation;
        playAnimation= [[NSArray alloc] initWithObjects:
                        [UIImage imageNamed:@“image1.png"],
                        [UIImage imageNamed:@“image2.png"],
                        [UIImage imageNamed:@"image3.png"],
                        [UIImage imageNamed:@"image4.png"],
                        [UIImage imageNamed:@"image5.png"],
                        [UIImage imageNamed:@"image6.png"],
                        [UIImage imageNamed:@"image7.png"],
                        Nil];

    }
        completion:^(BOOL finished) {

    UIStoryboard *Scene1 = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *Sc1 = [Scene1 instantiateViewControllerWithIdentifier:@"ViewController"];
        [self presentViewController:Sc1 animated:NO completion:nil];

    }];

}

非常感谢你的帮助

3 个答案:

答案 0 :(得分:0)

大多数animate *方法都有兄弟形式,需要一个完成块。使用完成来做任何你想做的事。

e.g。

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;

- 与 -

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

答案 1 :(得分:0)

取决于你如何开始制作动画。阅读chaining UIView animations,了解该主题的概况。

如果您正在使用UIView动画,请设置动画委托和animationDidStopSelector,例如:

[UIView beginAnimations:@"Grow" context:self.btn];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(doneAnimating:finished:context:)];
// Do stuff you want animated
[UIView commitAnimations];

如果您正在使用CABasicAnimation,同样的事情,只是一个不同的接口 - 设置委托和animationDidStop选择器,例如:

CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
anim.delegate = self;
anim.animationDidStop = @selector(doneAnimating:finished:context:)];
anim.fromValue = [NSNumber numberWithDouble:0.01];
anim.toValue = [NSNumber numberWithDouble:1.0];
anim.duration = 0.5f;
[layer addAnimation:anim forKey:@"grow"];

在任何一种情况下,动画完成后都会调用-doneAnimating:

-(void)doneAnimating:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    // Do whatever you want next
}

答案 2 :(得分:0)

你可以做基本的UIView动画。这些包括可以推送/显示视图的完成块。

[UIView animateWithDuration:1.5f animations:^{
        // Perform animation here.
        // ex.
        [label setFrame:CGRectMake(0, 0, 320, 50)];
    } completion:^(BOOL finished) {
        // Show new view.
       [self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
    }];