【问题标题】:How to close a UIView with an animation如何用动画关闭 UIView
【发布时间】:2015-03-25 03:47:54
【问题描述】:

当一个按钮被点击时,我会显示一个UIView,它会动画并向上滑动。

现在,当我单击按钮时,方法 buttoncall 被调用。 我想用动画关闭(向下移动/关闭)视图。 我该怎么做?

    v = [[MyView alloc] initWithFrame:mycell.bounds];

    [v.flipLowButton addTarget:self action:@selector(buttoncall:) forControlEvents:UIControlEventTouchUpInside];

    [mycell addSubview:v];

    CGRect r=v.frame;

    r.origin.y= mycell.frame.size.height;

    v.frame=r;

    rect.origin.y=r.origin.y-r.size.height;

    [UIView animateWithDuration:.2 animations:^{

     v.frame=r;    }];


- (void) buttoncall:(UIButton *)sender {

   // How to bring down the menu.

}

【问题讨论】:

  • 与显示菜单的操作相反。

标签: ios objective-c


【解决方案1】:

只需将视图降低其自身的高度即可。 例如:如果视图显示在距底部上方 100px 处,则只需将视图偏移 +100px 即可获得关闭效果。

[UIView animateWithDuration:.2 animations:^{

     [v setFrame:CGRectOffset(v.frame, 0, CGRectGetHeight(v.bounds))];    
}];

【讨论】:

    【解决方案2】:

    您可以使用已有的相同动画代码:

    [UIView animateWithDuration:.2 animations:^{
         v.frame=r;    
    }];
    

    只需将v.frame 设置为新的(或原来的)目的地即可。

    【讨论】:

    • 动画是从下到上的。而不是从上到下
    【解决方案3】:

    yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y, yourView.frame.size.width, yourView.frame.size.height);

    [UIView animateWithDuration:1.0 动画:^{

    yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y + yourView.frame.size.height, yourView.frame.size.width, yourView.frame.size.height);

    }];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多