简体   繁体   中英

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

在此处输入图片说明

Second Press it disappears with no animation

Third Press it animates to 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

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;
                     }];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM