UIView transitionWithView不起作用 [英] UIView transitionWithView doesn't work

查看:96
本文介绍了UIView transitionWithView不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是测试项目中主视图控制器的viewDidLoad:

Here is viewDidLoad from the main view controller in the test project:

- (void)viewDidLoad

{
[super viewDidLoad];

{ [super viewDidLoad];

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
[self.view addSubview:containerView];

UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[redView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:redView];

UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[yellowView setBackgroundColor:[UIColor yellowColor]];


[UIView transitionWithView:containerView duration:3
                   options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
                       [redView removeFromSuperview];
                       [containerView addSubview:yellowView];
                   }
                completion:NULL];
}

出现黄色框。没有动画,无论我尝试哪种UIViewAnimationOption。为什么???

The yellow box just appears. There is no animation, no matter which UIViewAnimationOption I try. Why???

编辑:我也尝试使用performSelector withDelay将动画移出viewDidLoad并转移到另一个方法中。相同的结果 - 没有动画。

I've also tried using performSelector withDelay to move the animation out of viewDidLoad and into another method. Same result - no animation.

也试过这个:
[UIView transitionFromView:redView toView:yellowView持续时间:3个选项:UIViewAnimationOptionTransitionFlipFromLeft完成:NULL];

Also tried this: [UIView transitionFromView:redView toView:yellowView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];

然而,黄色视图才会出现。没有动画。

Still, the yellow view just appears. No animation.

推荐答案

经过一些测试后,您似乎无法创建容器视图并在同一个runloop中设置动画让事情奏效。为了使你的代码有效,我首先在中创建 containerView redView viewDidLoad 方法。然后我将你的动画放入 viewDidAppear 方法。这样我就可以从 viewDidAppear containerView redView c>方法,我把它们作为属性。以下是 ST_ViewController.m 文件的代码,该文件将执行您想要的动画。

After some testing it appears that you cannot create the container view and set up the animation within the same runloop and have things work. In order to make your code work, I first created the containerView and the redView in the viewDidLoad method. Then I put your animation into the viewDidAppear method. So that I could reference the containerView and the redView from the viewDidAppear method, I made them properties. Here is the code for an ST_ViewController.m file that will perform the animation you wanted.

@interface ST_ViewController ()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *redView;
@end

@implementation ST_ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setContainerView:[[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)]];
    [[self view] addSubview:[self containerView]];

    [self setRedView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]];
    [[self redView] setBackgroundColor:[UIColor redColor]];
    [[self containerView] addSubview:[self redView]];
}

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];



    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    [yellowView setBackgroundColor:[UIColor yellowColor]];


    [UIView transitionWithView:[self containerView]
                      duration:3
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^(void){
                        [[self redView] removeFromSuperview];
                        [[self containerView] addSubview:yellowView];
                         }

                    completion:nil];

}

@end

这篇关于UIView transitionWithView不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆