如何在添加视图时添加动画?

时间:2013-06-18 13:03:41

标签: iphone ios ipad

我有一个应用程序,其中我在导航栏上有一个按钮。 点击时我需要在self.view中添加一个子视图。

DemoViewController *demoViewController1 = [[DemoViewController alloc] initWithNibName:@"DemoViewController" bundle:nil]; 
    CGRect frame=demoViewController1.view.frame;
    frame.origin.y = -300;
     [self.view addSubview:demoViewController1.view];
    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect frame1 = frame;
                         frame1.origin.y = 0;
                         demoViewController1.view.frame = frame1;
                     }];

我尝试通过设置添加视图的帧起点y(它是另一个视图控制器)。来一些negetive值并通过点击将它带到0.但它没有下拉效果。

我需要添加带有下拉视图等动画的视图。再次按下按钮时也需要像这样删除。

有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

您将视图添加为子视图,其中的帧不在屏幕上(例如负y坐标)并修改动画帧:

MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(50, -200, 200, 200)];
[self.view addSubview:myView];
[UIView animateWithDuration:0.3
                 animations:^{
                     CGRect frame = myView.frame;
                     frame.origin.y = 0;
                     myView.frame = frame;
                 }];

删除代码:

[UIView animateWithDuration:0.3
                 animations:^{
                     CGRect frame = myView.frame;
                     frame.origin.y = -1 * frame.size.height;
                     myView.frame = frame;
                 }
                 completion:^(BOOL finished) {
                     [myView removeFromSuperview];
                 }];

修改

用以下代码替换您的代码:

DemoViewController *demoViewController1 = [[DemoViewController alloc] initWithNibName:@"DemoViewController" bundle:nil]; 
    CGRect frame=demoViewController1.view.frame;
    frame.origin.y = -300;
    demoViewController1.view.frame = frame; // You forgot this line
     [self.view addSubview:demoViewController1.view];
    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect frame1 = demoViewController1.view.frame;
                         frame1.origin.y = 0;
                         demoViewController1.view.frame = frame1;
                     }];

答案 1 :(得分:0)

首先在底部保留你的视图 当它第一次点击时

这样做

[UIView animateWithDuration:0.3 animations:^{
//keep points that's in a screen
        self.view.center = CGPointMake(x,y,width,height);
}];

第二次点击时 在可见屏幕下面给出分数

[UIView animateWithDuration:0.3 animations:^{
//keep points that's in a screen
        self.view.center = CGPointMake(x,y,width,height);
}];

如果你找到比这更好的方法,请告诉我。

答案 2 :(得分:0)

[self.yourView setFrame:setYourInitialFrame];
[UIView animateWithDuration: 1.00 animations:^{
    [self.yourView setFrame:setYourFinalFrame];
}];