2017-05-31 68 views
-1

我有一個標籤。我有3根琴絃。我需要在無限循環中以10秒的延遲在相同的標籤中顯示3個字符串的文本。我如何用swift 3中的簡單動畫解決這個問題?標籤中的文本在swift中的時間延遲3

+0

您的字符串被聲明爲「Array」。你會想從它們中刪除括號。例如:'let str1:String =「專業和最佳學習中心」' – Adrian

+0

好的我現在怎麼辦? @阿德里安 – Tamannah

回答

0

這將適用於你的,正確連接你的插座,並在數組中聲明這些字符串,並加載定時器更改。

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var label: UILabel! 

var array = ["Aaaaaaaaaaa", "Bbbbbbbbbbb", "Ccccccccccc"] 
var scrollIndex = 0 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.myDelayedFunction), userInfo: nil, repeats: true) 
    timer.fire() 
} 

func myDelayedFunction()-> Void { 
let count = self.array.count 
    if scrollIndex == count { 
     scrollIndex = 0 
    } 
    if scrollIndex < count { 
     if count > 1{ 
      self.label.text = array[scrollIndex] 
      self.scrollIndex += 1 
     } 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
} 
+0

FUNC myDelayedFunction() - >空隙{ 令計數= self.array.count 如果scrollIndex ==計數{ scrollIndex = 0 } 如果scrollIndex <計數{ 如果計數> 1 { self.label .text = array [scrollIndex] self.scrollIndex + = 1 }}這一個是正確的。你可以編輯你的答案。 @Subrat Padhi – Tamannah

1

這是我的解決方案。只需將UILabel連接到IBOutlet

class ViewController: UIViewController { 

    @IBOutlet weak var textLabel: UILabel! 
    let messages = ["PROFESSIONAL AND BEST LEARNING CENTER","LEARNING TECHNOLOGY AND DESIGN IN A SMART WAY","EXPLORE YOUR SKILLS"] 
    let delayTime = 10.0 
    var counter = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let timer = Timer.scheduledTimer(timeInterval: delayTime, target: self, selector: #selector(changeDisplayedText), userInfo: nil, repeats: true) 
     timer.fire() 
    } 

    func changeDisplayedText() { 
     textLabel.text = messages[counter % messages.count] 
     counter += 1 
    } 
}