Picker View not appearing on 1st attempt of button click

夙愿已清 提交于 2019-12-08 09:56:21

问题


I have a button on the extreme right of the cell.

Now I have a picker view which needs to be displayed on button click.

Its working fine with the following code:

-(IBAction)buttonPressed:(id)sender
{
[UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 200);
    monthPicker.transform = transform;
    [self.view addSubview:monthPicker];
    [UIView commitAnimations];  
}

But I searched for making picker view hide when it is visible and be shown when no picker view is present,I found out a solution for it.....added the following code to above method:

monthPicker.hidden = [monthPicker isHidden] ? NO : YES;

Now the picker view is not getting displayed on 1st attempt when I click button,but it's working perfect from 2nd attempt and later.

Any suggestions?


回答1:


The answer from @Padavan should work, but if you want fading effect "in animation", hidden property does not do the job. You have to use UIView.alpha property instead. Here is my sample code for that.

- (void) viewDidLoad {
       [super viewDidLoad];

       /////////////////////////////////////////////////
       //    ... Initialize your month picker here    //
       /////////////////////////////////////////////////

       monthPicker.alpha = 0;    
       [self.view addSubview:monthPicker];    
}

- (void)buttonPressed:(id)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 200);
    monthPicker.transform = transform;
    monthPicker.alpha = monthPicker.alpha * (-1) + 1;
    [UIView commitAnimations];  
}



回答2:


Are monthPicker initially hidden? When method called first time. If no - it hides and you can't see it. Maybe you need set monthPicker.hidden=YES in init method.



来源:https://stackoverflow.com/questions/8617164/picker-view-not-appearing-on-1st-attempt-of-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!