动画UIPickerView不以横向模式旋转

时间:2011-07-14 20:39:31

标签: iphone objective-c ipad uipickerview

我有一个UIPickerView,可以在UITableViewController中上下动画。我改编了一个来自apple(DateCell)的例子。 UIPickerView是以编程方式创建的,没有nib文件。

在肖像模式下,一切看起来都不错。当我旋转模拟器时(我没有在设备上进行测试),UITableView旋转得很好,但拾取器仍然存在。我阅读了大量有关该主题的帖子,许多开发人员似乎都遇到了景观模式中拾取器表现奇怪但至少他们的选择器旋转的问题。我按照此链接http://www.llamagraphics.com/developer/using-uidatepicker-landscape-mode中所述创建了UIPickerView的子类,但它对轮换问题没有帮助。

我尝试用变换旋转拾取器,但它看起来非常奇怪,就像破碎一样。

我怀疑问题是我在UITableViewController中使用了选择器,因此我将其添加为self.view.window的子视图。如果我尝试将选择器添加为self.view的子视图,则只显示白框(没有任何选取器)。

有什么建议吗?

UITableViewController子类中的初始化代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.pickerView.superview == nil)
    {
        //Initialization code 
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        CGRect initFrame = orientation == UIInterfaceOrientationPortrait ? CGRectMake(0, 0, 320, 200) : CGRectMake(0, 0, 480, 160);

        self.pickerView = [[RotatingUIPickerView alloc] initWithFrame:initFrame];
        [self.pickerView setDelegate:self];
        [self.pickerView setShowsSelectionIndicator:YES];
        [self.pickerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];

        [self.view.window addSubview:self.pickerView];

        // compute the start frame
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
        CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
        CGRect startRect = CGRectMake(0.0,
                                      screenRect.origin.y + screenRect.size.height,
                                      pickerSize.width, pickerSize.height);

        [self.pickerView setFrame:startRect];

        // compute the end frame
        CGRect pickerRect = CGRectMake(0.0,
                                       screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                       pickerSize.width,
                                       pickerSize.height);

        // start the slide up animation
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];

        // we need to perform some post operations after the animation is complete
        [UIView setAnimationDelegate:self];

        [self.pickerView setFrame:pickerRect];

        [UIView commitAnimations];
    }
}

UIPicker子类的实现,如上面的链接所述:

- (id)initWithFrame:(CGRect)frame 
{
    if (self == [super initWithFrame:frame]) 
    {
        for (UIView * subview in self.subviews) 
        {
            [subview setFrame:self.bounds];
        }
    }
    return self;
}

- (id) initWithCoder:(NSCoder *)aDecoder 
{
    if (self == [super initWithCoder: aDecoder]) 
    {
        for (UIView * subview in self.subviews) 
        {
            [subview setFrame:self.bounds];
        }
    }
    return self;
}

1 个答案:

答案 0 :(得分:4)

执行:

 [self.view.window addSubview:self.pickerView];

您要将pickerView添加为UIWindow的子视图。我不知道主UIWindow如何旋转,如果可以的话。

pickerView添加到视图

 [self.view addSubview:self.pickerView];

由于视图为UITableView,您会遇到问题。

我建议将pickerView添加到某个中间UIView,作为子视图添加到UIWindow并添加UITableView。此中间UIView也会与UIViewController关联,以便shouldAutorotateToInterfaceOrientation可以返回正确的值以使自动轮换有效。