问题
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