简体   繁体   English

UIView transitionWithView不起作用

[英]UIView transitionWithView doesn't work

Here is viewDidLoad from the main view controller in the test project: 这是测试项目中主视图控制器的viewDidLoad:

- (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];
}

The yellow box just appears. 刚出现黄色框。 There is no animation, no matter which UIViewAnimationOption I try. 没有动画,无论我尝试哪种UIViewAnimationOption。 Why??? 为什么???

EDIT: I've also tried using performSelector withDelay to move the animation out of viewDidLoad and into another method. 编辑:我也尝试使用performSelector withDelay将动画移出viewDidLoad并转移到另一个方法。 Same result - no animation. 相同的结果 - 没有动画。

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

Still, the yellow view just appears. 尽管如此,黄色视图才会出现。 No animation. 没有动画。

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. 经过一些测试后,您似乎无法创建容器视图并在同一个runloop中设置动画并使其工作正常。 In order to make your code work, I first created the containerView and the redView in the viewDidLoad method. 为了使代码有效,我首先在viewDidLoad方法中创建了containerViewredView Then I put your animation into the viewDidAppear method. 然后我将你的动画放入viewDidAppear方法。 So that I could reference the containerView and the redView from the viewDidAppear method, I made them properties. 因此我可以从viewDidAppear方法引用containerViewredView ,我将它们作为属性。 Here is the code for an ST_ViewController.m file that will perform the animation you wanted. 以下是将执行所需动画的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

Don't put this in viewDidLoad . 不要把它放在viewDidLoad viewDidLoad is called when the view is fully loaded into memory, but not yet displayed on screen. 当视图完全加载到内存中但尚未显示在屏幕上时,将调用viewDidLoad

Try putting the above code in viewDidAppear: which is called when the view has appeared on screen. 尝试将上面的代码放在viewDidAppear:当视图出现在屏幕上时调用。

Edit: a side note, if performSelector:withDelay: fixes something, that means you're trying to do that thing wrong. 编辑:旁注,如果performSelector:withDelay:修复了一些东西,这意味着你试图做错了。 You need to refactor somehow. 你需要以某种方式重构。 performSelector:withDelay: is the wrong way to solve problems 99.999% of the time. performSelector:withDelay:99.999%的时间是解决问题的错误方法。 As soon as you place this code on a different speed device (new iPhone, old iPhone) it will mess up. 只要将此代码放在不同的速度设备(新的iPhone,旧的iPhone)上,它就会搞砸。

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

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