Search code examples
objective-ciosxcodesdkuipickerview

Checking if the UIPickerView is shown or hidden? The *if* conditions?


I have a little problem with my UIPickerView animation. I did set up the animation so when I click a button the UIPickerView will slide up and after another click it will slide down. But I hit the problem on if. I tried to set up a new settings bool and set it's value after each animation, so the if was just checking for the bool. I'm not sure if I did explained it well, but maybe you get the idea from the code. But unfortunately it's not working... Any ideas?

- (void)viewDidLoad
{
    [super viewDidLoad];

    settings = [NSUserDefaults standardUserDefaults];
    [settings setBool:NO forKey:@"pickerShown"];
    [settings synchronize];
}


- (IBAction)showPicker:(id)sender {

    CGRect rect = countryPicker.frame;
    CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.

    rect.origin = origin;
    countryPicker.frame = rect;


    if ([settings boolForKey:@"pickerShown"] == YES) {


        // Perform transform to slide it off the screen.
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        countryPicker.transform = CGAffineTransformMakeTranslation(0, -0); // Offset.
        [UIView commitAnimations];
        [settings setBool:NO forKey:@"pickerShown"];
        [settings synchronize];

    } else if ([settings boolForKey:@"pickerShown"] == NO) {


        // Perform transform to slide it onto the screen.
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
        [UIView commitAnimations];
        [settings setBool:YES forKey:@"pickerShown"];
        [settings synchronize];

    }
}

EDIT:

Just found the solution... If anyone's interested - here it is:

The h. file:

.
.
.
{
BOOL pickerShown;
}
.
.
.

The .m file:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect rect = countryPicker.frame;
    CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.

    rect.origin = origin;
    countryPicker.frame = rect;
pickerShown = NO;


}


- (IBAction)showPicker:(id)sender {


    if (pickerShown == NO) {


        // Perform transform to slide it onto the screen.
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        //[UIView setAnimationDidStopSelector:@selector(hidePicker)];
        countryPicker.transform = CGAffineTransformMakeTranslation(0, 0); // Offset.
        [UIView commitAnimations];
        [self performSelector:@selector(hidePicker) withObject:nil afterDelay:1];

    } else if (pickerShown == YES) {

        //countryPicker.hidden = NO;
        // Perform transform to slide it onto the screen.
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1];
        countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
        [UIView commitAnimations];
        countryPicker.hidden = NO;

    }
}

Solution

  • Just found the solution... If anyone's interested - here it is:

    The h. file:

    .
    .
    .
    {
    BOOL pickerShown;
    }
    .
    .
    .
    

    The .m file:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        CGRect rect = countryPicker.frame;
        CGPoint origin = CGPointMake(0, 510); // Some off-screen y-offset here.
    
        rect.origin = origin;
        countryPicker.frame = rect;
    pickerShown = NO;
    
    
    }
    
    
    - (IBAction)showPicker:(id)sender {
    
    
        if (pickerShown == NO) {
    
    
            // Perform transform to slide it onto the screen.
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:1];
            //[UIView setAnimationDidStopSelector:@selector(hidePicker)];
            countryPicker.transform = CGAffineTransformMakeTranslation(0, 0); // Offset.
            [UIView commitAnimations];
            [self performSelector:@selector(hidePicker) withObject:nil afterDelay:1];
    
        } else if (pickerShown == YES) {
    
            //countryPicker.hidden = NO;
            // Perform transform to slide it onto the screen.
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:1];
            countryPicker.transform = CGAffineTransformMakeTranslation(0, -266); // Offset.
            [UIView commitAnimations];
            countryPicker.hidden = NO;
    
        }
    }