views:

197

answers:

1

I try to remove a view from it's superview after being animated offscreen. But it seems when I add the removeFromSuperview call after when the animation is supposed to end, the view would no animate at all but instead disappear instantly from the screen.
So why is there no animation when I add the [[self pickerView] removeFromSuperview]; to the method below ?

- (void) slidePickerOutView
{

    [UIView beginAnimations:@"slidePickerOutView" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDidStopSelector:@selector(slidePickerOutViewEnded:finished:context:)];

    CGRect r = CGRectMake(0, 480, 320, 244);

    [[self pickerView] setFrame:r];

    [UIView commitAnimations];
}

- (void) slidePickerOutViewEnded:(NSString *)id finished:(BOOL) finished context:(void *) context 
{
    //Stop observing the 'Done' button
    [[self pickerView] removeObserver:self forKeyPath:@"valueSelectDone"];
    [[self pickerView] removeObserver:self forKeyPath:@"selectedValue"];

    [[self pickerView] removeFromSuperview];

    [self setPickerView:nil];
}
A: 

Ok, I eventually found what was causing the animation not to start. The pickerView was being observed and whenever one if it's instance variables would change of value, the parent view would get notified and would start the animation to slide the pickerview offscreen. But since the parentView was observing both old and new value, it got notified two times, first for the old and then for the new changed value. Thus the parentView was starting the animation two times right after each other as well, apparently this was causing the animation not to start at all.

fixed like this, just by commenting out the firstline which was sending the old value to its observer each time ValueSelectDone was changing value:

//[self willChangeValueForKey:@"valueSelectDone"];

valueSelectDone = flag;

[self didChangeValueForKey:@"valueSelectDone"]; 
Oysio