UIView的transitionWithView不起作用

发布于 2024-12-07 09:06:09 字数 1185 浏览 0 评论 0原文

这是测试项目中主视图控制器的 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 持续时间:3 个选项:UIViewAnimationOptionTransitionFlipFromLeft 完成:NULL];

尽管如此,黄色视图还是出现了。没有动画。

Here is viewDidLoad from the main view controller in the test project:

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

The yellow box just appears. There is no animation, no matter which UIViewAnimationOption I try. Why???

EDIT: I've also tried using performSelector withDelay to move the animation out of viewDidLoad and into another method. Same result - no animation.

Also tried this:
[UIView transitionFromView:redView toView:yellowView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];

Still, the yellow view just appears. No animation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-12-14 09:06:09

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

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. In order to make your code work, I first created the containerView and the redView in the viewDidLoad method. Then I put your animation into the viewDidAppear method. So that I could reference the containerView and the redView from the viewDidAppear method, I made them properties. Here is the code for an ST_ViewController.m file that will perform the animation you wanted.

@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
情深缘浅 2024-12-14 09:06:09

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

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

编辑:附注,如果 PerformSelector:withDelay: 修复了某些内容,则意味着您试图做错那件事。你需要以某种方式重构。 PerformSelector:withDelay: 在 99.999% 的情况下都是错误的解决问题的方法。一旦你将此代码放在不同速度的设备(新 iPhone、旧 iPhone)上,它就会变得混乱。

Don't put this in viewDidLoad. viewDidLoad is called when the view is fully loaded into memory, but not yet displayed on screen.

Try putting the above code in viewDidAppear: which is called when the view has appeared on screen.

Edit: a side note, if performSelector:withDelay: fixes something, that means you're trying to do that thing wrong. You need to refactor somehow. performSelector:withDelay: is the wrong way to solve problems 99.999% of the time. As soon as you place this code on a different speed device (new iPhone, old iPhone) it will mess up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文