【问题标题】:addSubview animation添加子视图动画
【发布时间】:2011-01-21 04:46:29
【问题描述】:

我有主 UIView,我在其中显示不同的数据。然后我放了一个按钮,它显示这样的子视图:

- (IBAction) buttonClicked:(id)sender
{
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,25,50,25)];
    newLabel.text = @"some text";
    [newView addSubview:newLabel];

    [self.view addSubview:newView];
    [newLabel release];
    [newView release];
}

newView 看起来不错,但它不会以任何方式为自己设置动画,它只是立即出现。 newView 出现时如何添加某种动画?像缩放或向下滑动或类似的东西。有一些简单的代码吗?

【问题讨论】:

    标签: iphone animation uiview


    【解决方案1】:
    [UIView transitionWithView:containerView duration:0.5
            options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like
            animations:^ { [containerView addSubview:subview]; }
            completion:nil];
    

    【讨论】:

    • 嘿@adedoy 如何在没有翻转的情况下使用正确的动画?像pushviewcontroller............
    【解决方案2】:

    您好,您可以使用 UIView 动画:

    [newView setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen!
    [UIView beginAnimations:@"animateTableView" context:nil];
    [UIView setAnimationDuration:0.4];
    [newView setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen!
    [UIView commitAnimations];
    

    SDK 现在会为您解决这个问题,并将视图动画化到您指定的位置。 这适用于 UIView 的大多数属性:alpha、scale、bounds、frames 等。

    还有内置动画如翻转和卷曲:

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                               forView:newView
                                cache:YES];
    
    [self.navigationController.view addSubview:settingsView.view];
    [UIView commitAnimations];
    

    希望这有助于您入门:)

    【讨论】:

    • 虽然第一个例子在技术上是有效的,但考虑到你今天需要支持的大量屏幕尺寸,它的形式很差,所以我建议第二个例子或@adedoy 的答案
    【解决方案3】:

    让我们快速完成。

    var redView = UIView(frame:CGRectMake(20,20,100,100))
    redView.backgroundColor = UIColor.redColor
    redView.alpha = 0.0
    view.addSubview(redView)
    
    UIView.animateWithDuration(0.25) { () -> Void in
        redView.alpha = 1.0
    }
    

    添加子视图不能通过简单地将其放在 animateWithDuration 块中进行动画处理。并且不能使用 hidden 设置动画。

    【讨论】:

      【解决方案4】:

      链接到 QuarzCore 框架

      #import <QuartzCore/QuartzCore.h>
      
      CATransition *transition = [CATransition animation];
      transition.duration = 1.0;
      transition.type = kCATransitionPush; //choose your animation
      [newView.layer addAnimation:transition forKey:nil];
      [self.view addSubview:newView]; 
      

      【讨论】:

      【解决方案5】:

      我发现即使使用动画块和选项,尝试单独为 addSubview 设置动画对我也无济于事。我找到了一种解决方法,即将子视图的 alpha 设置为 0.0,添加子视图,然后将 alpha 的设置从 0.0 设置为 1.0。这给了我想要的淡入淡出效果。

      希望这对其他人有所帮助。

      UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
      redView.backgroundColor = [UIColor redColor];
      redView.alpha = 0.0;
      
      [self.view addSubview:redView];
      
      [UIView animateWithDuration:0.5 animations:^{
        redView.alpha = 1.0;
      }];
      

      【讨论】:

      • 完美运行。但是,使用 Xcode 10 无法直接设置 alpha 组件。您需要分配一个带有 alpha 集的新 UIColor,例如[[UIColor redColor] colorWithAlphaComponent:0.0].
      • 感谢@AndreasKraft 我设置了带有 alpha 的颜色,想知道为什么 myView.alpha = 1 不起作用,您的评论帮助我弄清楚了。
      【解决方案6】:

      斯威夫特 5

      UIView.transition(with: renderView, duration: 0.25, options: .transitionCrossDissolve, animations: {
              self.renderView.addSubview(emojiView)
              self.renderView.bringSubviewToFront(emojiView)
          }, completion: nil)
      

      在代码示例中,renderView 是您将添加子视图的视图

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 2020-01-29
        • 2012-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多