2016-09-19 62 views
-1

我在使用addsubview创建按钮时遇到了动画我的按钮的问题。即使当我在循环中增加动画的延迟时,它们也会立即显示出来。我会感谢任何能够提供帮助的人。在swift中创建多个子视图时添加动画

func createButton() { 
    var setx = 50 
    var sety = 100 
    var delay = 0.4 
    var wordsInCharacters = [String]() 
    for letter in "RAILROAD".characters{ 
     wordsInCharacters.append("\(letter)") 
    } 
    while wordsInCharacters.count > 0 { 
     let randomIndex = Int(arc4random_uniform(UInt32(wordsInCharacters.count))) 
     // Creating the buttons 
     let button = SpringButton() 
     button.frame = CGRect(x: setx, y: sety, width: 64, height: 64) 
     button.setTitle("\(wordsInCharacters[randomIndex])", for: .normal) 
     button.setTitleColor(UIColor.white, for: .normal) 
     button.backgroundColor = UIColor.gray 
     button.titleLabel?.font = UIFont(name: "HelveticaNeue", size: 30) 
     // Add animation 
     UIView.animate(withDuration: 1, delay: delay, options: .curveEaseInOut, animations: { 
      self.view.addSubview(button) 
     }, completion: nil) 
     if setx <= 200 { 
      setx += 100 
     } 
     else{ 
      setx = 50 
      sety += 100 
     } 
     wordsInCharacters.remove(at: randomIndex) 
     delay += 0.2 

    } 
} 
+0

你想对按钮做什么样的动画?褪色? – xmhafiz

+1

问题是,“addSubview”不是一个神奇的动画的东西。只有动画视图属性可以在UIView.animate动画函数中动画。没有关于添加可动画的子视图的概念;它甚至不是一个可见的属性! – matt

回答

1

用于制作一个简单的动画不要在动画块中添加视图。 首先将Button Alpha值设置为0,然后将该按钮作为子视图添加。 在animationblock中,将button.alpha设置为1. Voila ...