ios - UIView transitionWithView 不起作用

标签 ios uiview uiviewcontroller uiviewanimationtransition

这是测试项目中主视图 Controller 的 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 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];

不过,黄色 View 只是出现了。没有动画。

最佳答案

经过一些测试后,您似乎无法在同一个运行循环中创建容器 View 和设置动画并让一切正常运行。为了使您的代码正常工作,我首先在 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

关于ios - UIView transitionWithView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616655/

相关文章:

ios - 在多次删除行时删除 UITableView 上的行中的错误

ios应用可以根据需要下载以下字体

iPhone - SVProgressHUD - 无法出现在 UIView 上

ios - 添加为另一个 View 的 subview 时未调用 touchesBegan 方法

ios - 将 URL 值传递给 SecondView Controller WebView (WKWebView)

ios - 如何跨两个 Storyboard执行自定义转场?

ios - 将从 PubNub 收到的消息转换为 Dictionary 对象

ios - 使用子对象遍历 NSDictionary

ios - 使用 Nib 和 UIView 类加载 UIView

ios - 从 View Controller 下的 UIView 类推送到 vc