0

我想使用动画从窗口底部加载视图,我知道这可以通过 presentmodalViewController 完成,但根据我的应用程序的要求,它无效,因为我只想在窗口的一半加载视图。所以我使用了动画,看看我做了什么

-(void) displayPicker
    {
        UIButton *done = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [done setFrame:CGRectMake(197, 199, 103, 37)];
        [done setBackgroundImage:[UIImage imageNamed:@"button_0.png"] forState:UIControlStateNormal];
        [done setTitle:@"Done" forState:UIControlStateNormal];
        [done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [done addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];

        [pickerDisplayView addSubview:datePicker];
        [pickerDisplayView addSubview:done];

    //animating here

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5];
        pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
        [self.view addSubview:pickerDisplayView];
        [UIView commitAnimations];

    }

名为 pickerDisplayView 的视图有两个名为 Picker 和 Button 的组件,但问题是 Picker 无法正常工作,并且视图 (pickerDisplayView) 都无法顺利加载。


@Matt:我按照你的指示做了,按照你的建议做了

-(void) animationIn
{
    CGPoint p = {x:0,y:185};

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    //pickerDisplayView = [[UIView alloc]initWithFrame:CGRectMake(0, 185, 320, 275)];
    [pickerDisplayView setCenter:p];
    [UIView commitAnimations];

}

但问题是视图在窗口顶部,而不是在 185 的确切 y 轴上。我从 IB 获取了这些坐标。请帮帮我先生

4

2 回答 2

0

你需要initaddSubview之前beginAnimations:。当您init pickerDisplayView定位它时,它的顶部与超级视图的底部是内联的。然后动画会向上滑动pickerDisplayView。理想情况下,您应该在 nib 或方法中创建视图loadView。(我强烈建议在“loadView”上使用笔尖)。

在 loadView 中执行此操作(或移至 nib):

CGRect offScreenFrame = ?????; //Figure this out from self.view
self.pickerDisplayView = [[UIView alloc]initWithFrame:offScreenFrame];
[self.view addSubview:self.pickerDisplayView];

这是动画:

[UIView beginAnimations:nil context:NULL];
self.pickerDisplayView.frame = CGRectMake(0, 185, 320, 275);
//[UIView setAnimationDuration:0.5]; //this line is probably not needed
[UIView commitAnimations];

我也建议不要在框架中使用“幻数”。相反,您可以pickerDisplayView从 superviews 框架派生框架。如果您确实使用了“神奇”数字,请务必以某种方式解释它们(即,将它们分配给适当命名的变量或注释)。

于 2010-11-12T14:03:42.217 回答
0

您必须在屏幕外添加视图,然后对其进行动画处理。

// In your viewDidLoad or wherever you initialize the view.
pickerDisplayView = [[UIView alloc]initWithFrame:offscreenRect];

[self.view addSubview:pickerDisplayView];
[self performSelector:@selector(animateIn) withObject:nil afterDelay:0.25];

// Declare animateIn
- (void)animateIn
{
    UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    [pickerDisplayView setCenter:onscreenPosition];
    [UIView commitAnimations];
}

该值offscreenRect是您希望视图开始的大小和位置的 CGRect。onscreenPosition是您希望动画完成的位置的 CGPoint。动画视图的中心就足够了,因为您不想在动画时更改视图的大小。

此外,我将动画放置在一个单独的方法中,该方法被调用-performSelector:withObject:afterDelay以确保动画着火。有时,如果您在添加动画视图的同一运行循环中启动动画,您将看不到动画运行。

于 2010-11-12T14:14:14.300 回答