8

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

- (void)viewDidLoad

{ [超级 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 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];

仍然,黄色视图刚刚出现。没有动画。

4

2 回答 2

6

经过一些测试后,您似乎无法在同一个运行循环中创建容器视图并设置动画并使其正常工作。为了使您的代码正常工作,我首先在方法中创建了containerView和。然后我将您的动画放入方法中。为了可以引用方法中的 the和 the ,我为它们设置了属性。这是执行所需动画的文件的代码。 redViewviewDidLoadviewDidAppearcontainerViewredViewviewDidAppearST_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
于 2012-10-18T17:52:24.070 回答
2

不要把这个放进去viewDidLoadviewDidLoad当视图完全加载到内存中但尚未显示在屏幕上时调用。

viewDidAppear:尝试将视图出现在屏幕上时调用的上述代码放入其中。

编辑:附带说明,如果 performSelector:withDelay: 修复了某些问题,这意味着您正在尝试做错事。你需要以某种方式重构。performSelector:withDelay: 在 99.999% 的情况下解决问题的方法都是错误的。一旦您将此代码放在不同速度的设备(新 iPhone、旧 iPhone)上,它就会一团糟。

于 2011-10-01T00:49:25.170 回答