【问题标题】:How to show/hide a UIView with animation in iOS?如何在 iOS 中显示/隐藏带有动画的 UIView?
【发布时间】:2011-12-17 04:08:52
【问题描述】:

主 UIView 包含两个子视图 - UIView_1UIView_2
UIView_2中,有一个按钮可以显示或隐藏UIView_1
例如,当用户触摸按钮以显示UIView_1 时,UIView_1 将向下滑动,UIView_2 将随着过渡向下推
我对动画知之甚少。有人可以给我一些示例代码以供参考吗?
我应该使用 CGAffineTransformMakeTranslation 吗?
谢谢

【问题讨论】:

  • 这个问题的答案很容易通过堆栈溢出的绊脚石找到。不仅仅是一个问题或问题。请在发帖前使用 papa google 一次。
  • @Anil 这个问题相当老了;)我猜这是 2011 年的一个合法(非重复)问题;)
  • 哎呀..错过了那里的约会..我的错!!!

标签: iphone ios ipad uiview uiviewanimationtransition


【解决方案1】:

你不需要这么复杂的东西。只需更改视图的框架大小即可。

    NSTimeInterval animationDuration = /* determine length of animation */;
    CGRect newFrameSize = /* determine what the frame size should be */;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    theViewToChange.frame = newFrameSize;
    [UIView commitAnimations];

【讨论】:

    【解决方案2】:

    使用淡入/淡出效果简单地隐藏/显示 `

    /显示/

    sliderView.hidden = NO;
    sliderView.alpha = 0.1;
    [UIView animateWithDuration:0.25 animations:^{
        sliderView.alpha = 1.0f;
    } completion:^(BOOL finished) {
        // do some
    }];
    

    /隐藏/

    [UIView animateWithDuration:0.25 animations:^{
        sliderView.frame =  CGRectMake(130, 30, 0, 0);
        [sliderView setAlpha:0.1f];
    } completion:^(BOOL finished) {
        sliderView.hidden = YES;
    }];
    

    `

    【讨论】:

      【解决方案3】:

      这取决于您想用UIView_2 做什么。

      1. 在 Interface Builder 中将 UIView_1 放在 UIView_2 下方。

      2. 大小UIView_2 以占用UINavigationBar 下方的所有空间。

      3. 使用以下代码调整(使用uiview2_resized_rectUIView_2 的框架,或平移/移动UIView_2 的框架(使用uiview2_translated_rect):

      
      CGRect uiview1_original_rect = UIView_1.frame;
      CGRect uiview2_original_rect = UIView_2.frame;
      
      

      CGRect uiview2_translated_rect = CGRectMake(uiview2_original_rect.origin.x, uiview2_original_rect.origin.y+uiview1_original_rect.size.height, uiview2_original_rect.size.width, uiview2_original_rect.size.height);

      CGRect uiview2_resized_rect = CGRectMake(uiview2_original_rect.origin.x, uiview2_original_rect.origin.y+uiview1_original_rect.size.height, uiview2_original_rect.size.width, uiview2_original_rect.size.height-uiview1_original_rect.size.height);

      [UIView animateWithDuration:0.300 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState animations:^{ //uncomment this and comment out the other if you want to move UIView_2 down to show UIView_1 //UIView_2.frame = uiview2_translated_rect; UIView_2.frame = uiview2_resized_rect; } completion:^(BOOL finished) {

      }];

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 2019-01-16
        • 2011-07-29
        • 2021-04-24
        • 1970-01-01
        • 2011-10-29
        相关资源
        最近更新 更多