【问题标题】:Show and Fade UIImageView after 2 Seconds2 秒后显示和淡入 UIImageView
【发布时间】:2012-09-17 21:35:43
【问题描述】:

我正在为我的 iPhone 游戏开发一个通知系统,并希望在屏幕上弹出一个图像并在 2 秒后自动淡出。

  1. 用户点击了调用“popupImage”方法的按钮
  2. 图像出现在屏幕的指定位置,不需要淡入
  3. 图像在屏幕上停留 2 秒后自行淡出。

有没有办法做到这一点?提前谢谢。

【问题讨论】:

    标签: ios xcode animation uiimage fadeout


    【解决方案1】:

    为此使用UIView 专用方法。

    所以假设您已经准备好您的UIImageView,已经创建并添加到主视图中,但只是隐藏了。您的方法只需使其可见,并在 2 秒后启动动画以将其淡出,方法是将其“alpha”属性从 1.0 设置为 0.0(在 0.5 秒动画期间):

    -(IBAction)popupImage
    {
        imageView.hidden = NO;
        imageView.alpha = 1.0f;
        // Then fades it away after 2 seconds (the cross-fade animation will take 0.5s)
        [UIView animateWithDuration:0.5 delay:2.0 options:0 animations:^{
             // Animate the alpha value of your imageView from 1.0 to 0.0 here
             imageView.alpha = 0.0f;
         } completion:^(BOOL finished) {
             // Once the animation is completed and the alpha has gone to 0.0, hide the view for good
             imageView.hidden = YES;
         }];
    }
    

    就这么简单!

    【讨论】:

    • 谢谢这对我有帮助。
    • 除非您在视图消失后再次将 alpha 级别设置为 1,否则您将无法再次执行此操作。
    【解决方案2】:

    斯威夫特 2

    self.overlay.hidden = false
    UIView.animateWithDuration(2, delay: 5, options: UIViewAnimationOptions.TransitionFlipFromTop, animations: {
        self.overlay.alpha = 0
    }, completion: { finished in
        self.overlay.hidden = true
    })
    

    斯威夫特 3、4、5

    self.overlay.isHidden = false
    UIView.animate(withDuration: 2, delay: 5, options: UIView.AnimationOptions.transitionFlipFromTop, animations: {
        self.overlay.alpha = 0
    }, completion: { finished in
        self.overlay.isHidden = true
    })
    

    overlay 是我的形象的出口。

    【讨论】:

      【解决方案3】:

      @AliSoftware的回答的 Swift 3 版本

      imageView.isHidden = false
      imageView.alpha = 1.0
      
      UIView.animate(withDuration: 0.5, delay: 2.0, options: [], animations: {
      
                  self.imageView.alpha = 0.0
      
              }) { (finished: Bool) in
      
                  self.imageView.isHidden = true
              }
      

      【讨论】:

        【解决方案4】:

        是的,有。看看UIView 基于块的动画here。以谷歌为例。

        + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
        

        您也可以发起timer

        + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-30
          • 2013-09-11
          • 2013-10-07
          • 2011-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多