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 (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 theself.layerproperty 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: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 toloadNibNamed: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.