ios – UIView transitionWithView不起作用

前端之家收集整理的这篇文章主要介绍了ios – UIView transitionWithView不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是测试项目中主视图控制器的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,300)];
[redView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:redView];

UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0,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方法中创建了containerView和redView.然后我将你的动画放入viewDidAppear方法.因此我可以从viewDidAppear方法引用containerView和redView,我将它们作为属性.以下是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,300)]];
    [[self view] addSubview:[self containerView]];

    [self setRedView:[[UIView alloc] initWithFrame:CGRectMake(0,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,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
原文链接:https://www.f2er.com/iOS/331430.html

猜你在找的iOS相关文章