-3

我有一个应用程序,其中导航栏上有一个按钮。单击该按钮时,我需要向 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 设置为某个负值并单击将其设置为 0。但它没有那种下拉效果。

我需要添加带有动画的视图,例如下拉视图。并且当再次按下按钮时也需要像这样删除。

有人可以帮我吗?

4

3 回答 3

2

You add the view as subview with a frame that is not on the screen (e.g. negative y coordinate) and modify the frame animated:

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;
                 }];

And the code for removal:

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

Edit:

Replace your code with this:

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;
                     }];
于 2013-06-18T13:12:09.203 回答
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);
}];

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

于 2013-06-18T13:09:15.847 回答
0
[self.yourView setFrame:setYourInitialFrame];
[UIView animateWithDuration: 1.00 animations:^{
    [self.yourView setFrame:setYourFinalFrame];
}];
于 2013-06-18T13:15:12.020 回答