【问题标题】:iPhone: UIImageView Fades in - Howto?iPhone:UIImageView 淡入 - Howto?
【发布时间】:2011-08-03 16:40:02
【问题描述】:

我有一个UIImageView,我想淡入。

有没有办法在底层 UIViewController 上启用它?

我已经翻译了最简单的答案,尽管它们都有效,C# .NETMonoTouch 用户:

public override void ViewDidAppear (bool animated)
{
    base.ViewDidAppear (animated);
    UIView.BeginAnimations ("fade in");
    UIView.SetAnimationDuration (1);
    imageView.Alpha = 1;
    UIView.CommitAnimations ();
}

【问题讨论】:

    标签: iphone ios ipad uiimageview xamarin.ios


    【解决方案1】:

    将动画持续时间更改为您想要的任何长度。

    UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
    myImageView.center = CGPointMake(100, 100);
    myImageView.alpha = 0.0;
    [self.view addSubview:myImageView];
    [UIView animateWithDuration:5.0 animations:^{
         myImageView.alpha = 1.0;
    }];
    

    【讨论】:

    • 是否可以只为 UIImageView 而不是 UIView 设置动画? (如果是的话怎么做?)
    • 为我工作:我从 UI 编辑器中添加了一个 UIImageView,在 (我需要淡出图像视图) 之后只需添加最后 3 行例如 (with alpha = 0.0) 并且没问题!
    【解决方案2】:

    最初将您的 imageview 的 alpha 设置为 0 为 imageView.alpha = 0;

    - (void)fadeInImage 
    {
    [UIView beginAnimations:@"fade in" context:nil];
        [UIView setAnimationDuration:1.0];
        imageView.alpha = 1.0;
        [UIView commitAnimations];
    
    }
    

    【讨论】:

      【解决方案3】:

      在你的UIViewController下面使用

      // add the image view
      [self.view addSubview:myImageView];
      // set up a transition animation
      CATransition *animate = [CATransition animation];
      [animate setDuration:self.animationDelay];
      [animate setType:kCATransitionPush];
      [animate setSubtype:kCATransitionFade];
      [animate setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
      
      [[self layer] addAnimation:animate forKey:@"fade in"];
      

      【讨论】:

        【解决方案4】:

        这个简单的代码:

        [UIView animateWithDuration:5.0 animations:^{
                theImage.alpha = 1.0;
            }];
        

        UIImage 在视图中并通过IBOutlet 连接 您也可以从实用程序面板设置 alpha。

        【讨论】:

          【解决方案5】:

          我会使用 UIView 的 +transitionWithView:duration:options:animations:completion: 它非常高效和强大。

          [UIView transitionWithView:imgView duration:1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
              imgView.image = [UIImage imageNamed:@"MyImage"];
          } completion:nil];
          

          【讨论】:

            【解决方案6】:

            您可以使用此代码:

            淡入动画:

            self.yourComponent.alpha = 0.0f;
            [UIView beginAnimations:@"fadeIn" context:nil];
            [UIView setAnimationDuration:1.0]; // Time in seconds
            self.yourComponent.alpha = 1.0f;
            

            淡出动画:

            self.yourComponent.alpha = 1.0f;
            [UIView beginAnimations:@"fadeOut" context:nil];
            [UIView setAnimationDuration:1.0]; // Time in seconds
            self.yourComponent.alpha = 0.0f;
            

            self.yourComponent 可以是 UIViewUIImageViewUIButton 或任何其他组件。

            【讨论】:

              【解决方案7】:

              Swift 版本

              func fadeIn(){
                  UIView.beginAnimations("fade in", context: nil);
                  UIView.setAnimationDuration(1.0);
                  imageView.alpha = 1.0;
                  UIView.commitAnimations();
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-03-28
                • 2012-12-22
                • 2013-01-13
                相关资源
                最近更新 更多