Search code examples
iosobjective-ciphonexcodeuipickerview

UIPickerView Animate on and off screen behavior


This is a continuation of this question

CGRectMake : How To Calculate X and Y For UIPickerView Animation?

answered by https://stackoverflow.com/users/2315974/danypata

I have a picker view that I want to animate on and off screen on a button press and using the code in the previous question/answer I have got it to animate on screen the first time the button is pressed.

the second tome the button is pressed it just disappears and then the third time it is pressed it animates to the wrong place...please help

the code

if (self.pickerSort.hidden == NO) {


    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.pickerSort.frame = CGRectMake(0,self.view.frame.size.height
                                           +  self.pickerSort.frame.size.height,-self.pickerSort.frame.size.width,-self.pickerSort.frame.size.height);
    }
                     completion:^(BOOL finished) {
                     }];
    self.pickerSort.hidden = YES;
   // [self performSelector:@selector(hidePicker) withObject:nil afterDelay:1];

} else if (self.pickerSort.hidden == YES) {

    self.pickerSort.hidden = NO;

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.pickerSort.frame = CGRectMake(0 ,
                                       self.view.frame.size.height
                                       -  self.pickerSort.frame.size.height,
                                       self.pickerSort.frame.size.width,
                                       self.pickerSort.frame.size.height);
    }
                     completion:^(BOOL finished) {
                     }];
    [self.view bringSubviewToFront:self.pickerSort];

Behavior in images - button press animates onto screen beautifully

enter image description here

Second Press it disappears with no animation

Third Press it animates to here

enter image description here

Any help would be great...functionality I am looking for is how to put the picker view back on an animation so the 3rd press is the same as the first


Solution

  • After playing around with this and learning the ins and outs of frames and CGRects, I achieved this by using the following code

      if (pickerSort.hidden == YES) {
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            pickerSort.hidden = NO;
    
            visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
            [self.tableStations addSubview:visualEffectView];
            pickerSort.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height);
            visualEffectView.frame = CGRectMake(0,0,originalPickerFrame.size.width,originalPickerFrame.size.height - 64);
        }
                         completion:^(BOOL finished) {
    
                             self.navigationController.navigationItem.leftBarButtonItem.enabled = NO;
    
                         }];
    }
    else
    {
        visualEffectView.hidden = YES;
        [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    
            pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0,0);
        }
                         completion:^(BOOL finished) {
    
                             self.navigationController.navigationItem.leftBarButtonItem.enabled = YES;
                             pickerSort.hidden = YES;
    
                         }];
    }
    

    I set the original frame size in viewDidLoad like so

     pickerSort = [[UIPickerView alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
    originalPickerFrame = pickerSort.frame;
    
    pickerSort.frame = CGRectMake(0,-originalPickerFrame.size.height,0, 0);
    

    Hope this can help somebody in the future