检查是否显示或隐藏了UIPickerView? * if *条件?

时间:2011-10-18 18:41:45

标签: objective-c ios xcode sdk uipickerview

我的UIPickerView动画有点问题。 我确实设置了动画,所以当我点击一个按钮时,UIPickerView会向上滑动,再次点击它会向下滑动。但我在 if 上遇到了问题。我尝试设置一个新设置bool并在每个动画后设置它的值,所以 if 只是检查bool。我不确定我是否做得很好,但也许你从代码中得到了想法。 但不幸的是它没有用......有什么想法吗?

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

    }
}

编辑:

刚刚找到解决方案...... 如果有人有兴趣 - 这里是:

h。文件:

.
.
.
{
BOOL pickerShown;
}
.
.
.

.m文件:

- (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;

    }
}

4 个答案:

答案 0 :(得分:4)

如果视图在屏幕上可见,则window属性将为非零。

答案 1 :(得分:1)

刚刚找到解决方案......如果有人感兴趣 - 这里是:

h。文件:

.
.
.
{
BOOL pickerShown;
}
.
.
.

.m文件:

- (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;

    }
}

答案 2 :(得分:1)

您可以查看:

self.countryPicker.superview

如果选择器被隐藏,那么superview将为零。否则它将包含视图。

答案 3 :(得分:0)

如果你想要的只是显示选择器视图的基本效果,那么再次删除它会有一种更简单的方法。

伪代码:

//do any picker initialization here

if(!pickerIsShowing)
[self.view addSubview:yourPickerView];
else
 [self.yourPickerView removeFromSuperView];