检查 UIPickerView 是显示还是隐藏? *if* 条件?

发布于 2024-12-10 14:52:51 字数 2773 浏览 1 评论 0原文

我的 UIPickerView 动画有一点问题。 我确实设置了动画,因此当我单击按钮时 UIPickerView 将向上滑动,再次单击后它将向下滑动。但我在 if 上遇到了问题。我尝试设置一个新的设置布尔值,并在每个动画之后设置它的值,因此 if 只是检查布尔值。我不确定我是否解释得很好,但也许您从代码中得到了这个想法。 但不幸的是它不起作用......有什么想法吗?

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

    }
}

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;

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二智少女 2024-12-17 14:52:51

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

If a view is visible on screen, it's window property will be non-nil.

一萌ing 2024-12-17 14:52:51

您可以通过以下方式检查:

self.countryPicker.superview

如果选择器隐藏,则超级视图将为nil。否则它将包含视图。

You can check it with:

self.countryPicker.superview

If picker is hidden then superview will be nil. Otherwise it will be containing view.

冷月断魂刀 2024-12-17 14:52:51

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

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;

    }
}

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;

    }
}
别再吹冷风 2024-12-17 14:52:51

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

伪代码:

//do any picker initialization here

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

If all you want is the basic effect of displaying the picker view, then removing it again there is an easier way.

pseudocode:

//do any picker initialization here

if(!pickerIsShowing)
[self.view addSubview:yourPickerView];
else
 [self.yourPickerView removeFromSuperView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文