2017-08-15 4 views
0

私はUITextViewを持っていますが、私がしようとしているのは2秒後に別のテキストを追加することです。 2秒ごとにテキストが消え、別のテキストが表示されます。私は2つのテキストだけでそれをしましたが、もしそれ以上のものをしたいのであればどうしますか?一定の時間内にUITextViewからテキストを変更します

次のコードは、私はそれをやった方法を紹介します:

@IBOutlet weak var label: UILabel! 

var isTextOne = true 
var textOne: String = "one" 
var textTwo: String = "two" 
var textThree: String = "three" 

@IBOutlet weak var quoteView: UIView! 

var textTimer: Timer! 

func toggleText() { 
    label.text = isTextOne ? textTwo:textOne 
    isTextOne = !isTextOne 
    fadeViewInThenOut(view: quoteView, delay: 2) 
} 

func fadeViewInThenOut(view : UIView, delay: TimeInterval) { 
    let animationDuration = 0.25 

    // Fade in the view 
    UIView.animate(withDuration: animationDuration, animations: {() -> Void in 
     view.alpha = 1 
    }) { (Bool) -> Void in 
     // After the animation completes, fade out the view after a delay 

     UIView.animate(withDuration: animationDuration, delay: delay, options: .curveEaseInOut, animations: {() -> Void in 
      view.alpha = 0 
     }, completion: nil) 
    } 
} 

答えて

0

あなたがする必要がありますすべてのテキスト/単語を保持する単語のリストを作成します

  1. それを実装する必要がありますテキストビューで表示
  2. 範囲[0-(n-1)(nは表示される単語リストの合計サイズ)の範囲内の乱数を返すメソッドを作成する
  3. 2つごと秒または一定間隔で、上記のメソッドを呼び出すと、リスト内のその位置で単語を取得するために使用できる乱数が与えられます。
1

これは私のやり方です。

var Quote: String = "" 


    var gameTimer: Timer! 
    var textTimer: Tr!ime 

    func fadeViewInThenOut(view : UIView, delay: TimeInterval) { 

      let animationDuration = 0.25 

      // Fade in the view 
      UIView.animate(withDuration: animationDuration, animations: {() -> Void in 
       view.alpha = 1 
      }) { (Bool) -> Void in 

       // After the animation completes, fade out the view after a delay 

       UIView.animate(withDuration: animationDuration, delay: delay, options: .curveEaseInOut, animations: {() -> Void in 
        view.alpha = 0 
       }, 
          completion: nil) 
      } 
     } 

     func runTimedCode() { 

      fadeViewInThenOut(view: quoteView, delay: 0.7) 


      func randomNumber(_ arrayLength: Int) -> Int { 
       let unsignedArrayCount = UInt32(arrayLength) 
       let unsignedRandomNumber = arc4random_uniform(unsignedArrayCount) 
       let randomNumber = Int(unsignedRandomNumber) 


       return randomNumber 
      } 

      // Importing Quotes plist File 
      let quotes = ImportList(FileName: "QuotesList") 

      // Selects Quote 
      let chosenQuote: String = quotes.array[randomNumber(quotes.count())] as! String 

      // Assigns Quote & Author to IBOutlet 
      Quote = chosenQuote 

      label.text = Quote 

     } 

と私は負荷

gameTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(runTimedCode), userInfo: nil, repeats: true) 
をしたビューでこれを置きます
関連する問題