使用数组中的字符串实现标签淡入/淡出效果

4
func setOverlayTitle() {
    self.overlayLogo!.text = "Welcome"
    var hello: [String] = ["Bon Jour", "GUTEN\nMORGEN", "BONJOUR", "HOLA", "안녕하세요", "BUENOS DÍAS", "BUONGIORNO", "早安", "おはよう", "गुड मॉर्निंग"]

    for (var i = 1; i < hello.count-1; i++) {
    UIView.animateWithDuration(1.0, delay: 2.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
        self.overlayLogo!.alpha = 0.0
        }, completion: {
            (finished: Bool) -> Void in
            println(i)
            self.overlayLogo!.text = hello[i]

            // Fade in
            UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
                self.overlayLogo!.alpha = 1.0
                }, completion: nil)
    })
  }

}

我有以下代码,我想遍历数组“hello”,并在2秒延迟后淡入/淡出标签。但这段代码无法工作!目前它只打印数字9,9次,并且只淡入/淡出一次。

1
你用断点追踪过代码吗?哪些部分不起作用?有什么错误显示? - user3857868
你为什么从1开始i?应该是:for (var i = 0; i < hello.count-1; i++) - user3857868
@ShaanSingh 没有任何影响。 - Mike JS Choi
是的,我还在看你的代码。只是指出了一个快速修复的问题。 - user3857868
尝试通过硬编码更改文本:self.overlayLogo!.text =“test”。这样做有效吗?如果有效,那么问题就被缩小了。 - user3857868
显示剩余2条评论
1个回答

4
该问题是由您在此处使用的for循环引起的。您无法以这种方式获得所需的行为。 for循环不会等待您的标签动画完成后才开始下一次迭代。相反,您可以在每个动画周期完成后从方法本身进行递归调用。
您需要在您的类中声明一个变量,并像下面这样实现该方法: Swift 5及以上版本更新:
var iterator = 1;
    
override func viewDidLoad() {
        
   super.viewDidLoad()
   self.lblAnime!.text = "Welcome"
   setOverlayTitle()
}
    
func setOverlayTitle() {
   let hello: [String] = ["Bon Jour", "GUTEN\nMORGEN", "BONJOUR", "HOLA", "안녕하세요", "BUENOS DÍAS", "BUONGIORNO", "早安", "おはよう", "गुड मॉर्निंग"]
        
   UIView.animate(withDuration: 1.0, delay: 2.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
            
      self.lblAnime!.alpha = 0.0
   }, completion: { (finished: Bool) -> Void in
            
      self.lblAnime!.text = hello[self.iterator]
            
      // Fade in
      UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
                
         self.lblAnime!.alpha = 1.0
      }, completion: { (finished: Bool) -> Void in
                
         self.iterator += 1
         if self.iterator < hello.count {
            self.setOverlayTitle();
         }
      })
   })
}

原始答案(旧版本的Swift):

var iterator = 1;

override func viewDidLoad()
{
    
    super.viewDidLoad()
    self.lblAnime!.text = "Welcome"
    setOverlayTitle()
}

func setOverlayTitle()
{
    
    var hello: [String] = ["Bon Jour", "GUTEN\nMORGEN", "BONJOUR", "HOLA", "안녕하세요", "BUENOS DÍAS", "BUONGIORNO", "早安", "おはよう", "गुड मॉर्निंग"]
    
    UIView.animateWithDuration(1.0, delay: 2.0, options: UIViewAnimationOptions.CurveEaseOut, animations:
       {
            self.lblAnime!.alpha = 0.0
       },
       completion:
       {(finished: Bool) -> Void in
            println(self.iterator)
            self.lblAnime!.text = hello[self.iterator]
                
            // Fade in
            UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations:
                {
                     self.lblAnime!.alpha = 1.0
                },
                completion:
                {(finished: Bool) -> Void in
                     self.iterator++
                        
                     if self.iterator < hello.count
                     {
                         self.setOverlayTitle();
                     }
                })
       })
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接