简体   繁体   English

UIPickerView动画在屏幕上和屏幕外的行为

[英]UIPickerView Animate on and off screen behavior

This is a continuation of this question 这是这个问题的延续

CGRectMake : How To Calculate X and Y For UIPickerView Animation? CGRectMake:如何为UIPickerView动画计算X和Y?

answered by https://stackoverflow.com/users/2315974/danypata 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 在研究了这一点并了解了框架和CGRect的来龙去脉之后,我通过使用以下代码实现了这一点

  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 我像这样在viewDidLoad中设置原始帧大小

 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 希望这可以在将来对某人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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