简体   繁体   English

动画不起作用

[英]Animation doesn't work

I have a problem with an animation. 我的动画有问题。 The problem is that if I try to animate a view that is already created all goes well, if I try to create and animate a view at the same time the animation doesn't work. 问题是,如果我尝试为已经创建的视图设置动画,那么一切顺利,如果我尝试在动画不起作用的同时创建和动画视图。

Can anyone help me? 谁能帮我?

My Methods 我的方法

+ (LoginView *)sharedInstance {
    @synchronized(self) {
        if (nil == _sharedInstance) {
            _sharedInstance = (LoginView *)[[[NSBundle mainBundle] loadNibNamed:@"LoginView" owner:nil options:nil] objectAtIndex:0];
        }
    }
    return _sharedInstance;
}

- (void)hide:(BOOL)value animated:(BOOL)animated {
    CATransition * animation = [CATransition animation];

    animation.type = kCATransitionFade;
    [animation setDuration:1.0];

    if(_autoManageModalView)
        [animation setDelegate:self];

    [[self layer] removeAllAnimations];
    [[self layer] addAnimation:animation forKey:kCATransition];

    self.hidden = value;
}

How I call them 我怎么称呼他们

[[LoginView sharedInstance] hide:NO animated:YES];

The first time (with the same call) animation doesn't work, from the secondo time all goes well. 第一次(使用相同的调用)动画不起作用,从第二次开始一切顺利。 Thank in advance! 预先感谢!

You are animating your view too early in its lifecycle. 您在其生命周期中过早地为您的视图制作动画。 In theory, you create a view, then display it somewhere (eg, addSubview: ), then you animate it. 理论上,您创建一个视图,然后将其显示在某个地方(例如, addSubview: ,然后为其设置动画。

It is highly possible, though I have not checked it, that the first time that your hide:animated: method is called the self.layer property is null; 虽然我没有检查过,但很有可能第一次调用hide:animated:方法时self.layer属性为null; in any case, the animation would happen before the view is displayed, so you would not see it. 在任何情况下,动画都会视图显示之前发生,所以你不会看到它。

All in all, first display the view, then call the hide:animated: method on it. 总而言之,首先显示视图,然后调用hide:animated:方法。

After your comment: try and call the hide:animated: method through a method like: 评论之后:尝试通过以下方法调用hide:animated:方法:

 performSelector:withObject:afterDelay:

If you specify a 0.0 delay, this will simply queue the call to hide:animate: on the main loop, so that all the processing related to loadNibNamed: can happen and so give your view the time to be set up for display correctly. 如果指定0.0延迟,这将简单地将调用队列loadNibNamed: hide:animate:在主循环上,以便与loadNibNamed:相关的所有处理都可以发生,从而为视图设置正确显示的时间。

In order to use performSelector:withObject:afterDelay: you will need to modify your method signature so that it takes one argument and this must be an NSObject-derived type, not a primitive type. 为了使用performSelector:withObject:afterDelay:你需要修改你的方法签名,以便它接受一个参数,这必须是NSObject派生的类型,而不是基本类型。

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

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