0

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!

4

1 回答 1

2

You are animating your view too early in its lifecycle. In theory, you create a view, then display it somewhere (e.g., addSubview:), then you animate it.

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; 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.

After your comment: try and call the hide:animated: method through a method like:

 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.

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.

于 2012-06-05T08:39:44.443 回答