0

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

Community
  • 1
  • 1
JKX
  • 177
  • 2
  • 15

2 Answers2

0

Please try to use the below code.

    self.pickerSort.hidden = NO;
    NSTimeInterval duration = 0.5;
    [UIView animateWithDuration:duration
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         CGRect pickerFrame = self.pickerSort.frame;

                         // currently picker is off the screen, show it.
                         if (pickerFrame.origin.y == self.view.frame.size.height) {
                             // set frame to show picker
                             pickerFrame.origin.y = self.view.frame.size.height - self.pickerSort.frame.size.height;
                         } else {
                             // set frame to hide picker
                             pickerFrame.origin.y = self.view.frame.size.height;
                         }
                         self.pickerSort.frame = pickerFrame;
                     }
                     completion:^(BOOL finished) {
                         self.pickerSort.hidden = YES;
                     }];
swapnilagarwal
  • 1,126
  • 8
  • 16
  • This did not work for me but lead me in the right direction somewhat rubber duck style...thank you – JKX May 15 '15 at 14:01
0

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

JKX
  • 177
  • 2
  • 15