0

我试图在 UIPicker 上做一个动画,当我的应用程序运行它的隐藏时,当按下按钮时,它会出现从按钮滑动,当我再次点击按钮时,它会滑开。好吧,我的代码做到了,但只有一次。它确实:

首先单击 -> 显示选择器,isPickerHidden = NO

第二次点击 -> 隐藏选择器,isPickerHidden = YES

第三次单击-> 不执行任何操作,但返回 isPickerHidden = NO where in viewDidLoad 声明;

和第四次点击一样,它只返回正确的 BOOL 值。

isPickerHidden = YES;

[self.picker setHidden:isPickerHidden];

if(isPickerHidden == NO){
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.6];
        CGRect frame = self.picker.frame;
        [self.picker setFrame:CGRectOffset(frame, self.picker.frame.origin.x, self.picker.frame.origin.y)];
        [UIView commitAnimations];
        isPickerHidden = YES;
        NSLog(@"hidden yes");
    }else if(isPickerHidden == YES) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.6];
        CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, -200);
        self.picker.transform = transfrom;
        [self.picker setHidden:NO];
        [UIView commitAnimations];
        NSLog(@"hidden no ");
        isPickerHidden = NO;
    }

我想知道我面临的问题是什么,我错过了什么。谢谢

4

2 回答 2

0

为框架和选择器添加一些日志记录以查看发生了什么...

    NSLog(@"picker: %.0f, %.0f, %.0f, %.0f", self.picker.frame.origin.x, self.picker.frame.origin.y, self.picker.frame.size.width, self.picker.frame.size.height);
于 2013-08-07T13:27:06.860 回答
0

感谢 NS 兄弟的回复,但我现在采用了不同的方法,而不是从我使用 CGRectMake 的原始方法中偏移点,我这样做了

if(isPickerHidden == NO) {
        CGRect frame = self.picker.frame;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.6];

        [self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y + 216, frame.size.width, frame.size.height)];
        [UIView commitAnimations];
        isPickerHidden = YES;

    } else if (isPickerHidden == YES) {
        CGRect frame = self.picker.frame;
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.6];

        [self.picker setFrame:CGRectMake(frame.origin.x, frame.origin.y - 216, frame.size.width, frame.size.height)];
        [UIView commitAnimations];
        isPickerHidden = NO;

    }

向下滑动和向上滑动都以相同的方式动画效果很好。但现在的问题是我想让 UIPicker 在应用程序加载时消失,所以在 viewDidLoad 我确实尝试使用 [self.picker setHidden:YES]; 但是当我按下按钮时,我使用 CGRect 制作的帧没有出现,我也尝试了 alpha 仍然没有。

我也确实在 if-else 语句的主体中将 setHidden 设置为 yes 并了解它们的值。想知道我现在缺少什么。

于 2013-08-08T13:07:11.327 回答