繁体   English   中英

UIView transitionWithView不起作用

[英]UIView transitionWithView doesn't work

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

- (void)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。 为什么???

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

还尝试了这个:[UIView transitionFromView:redView toView:yellowView duration:3选项:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];

尽管如此,黄色视图才会出现。 没有动画。

经过一些测试后,您似乎无法创建容器视图并在同一个runloop中设置动画并使其工作正常。 为了使代码有效,我首先在viewDidLoad方法中创建了containerViewredView 然后我将你的动画放入viewDidAppear方法。 因此我可以从viewDidAppear方法引用containerViewredView ,我将它们作为属性。 以下是将执行所需动画的ST_ViewController.m文件的代码。

@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

不要把它放在viewDidLoad 当视图完全加载到内存中但尚未显示在屏幕上时,将调用viewDidLoad

尝试将上面的代码放在viewDidAppear:当视图出现在屏幕上时调用。

编辑:旁注,如果performSelector:withDelay:修复了一些东西,这意味着你试图做错了。 你需要以某种方式重构。 performSelector:withDelay:99.999%的时间是解决问题的错误方法。 只要将此代码放在不同的速度设备(新的iPhone,旧的iPhone)上,它就会搞砸。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM