按顺序显示数组中的每个元素

问题描述 投票:0回答:1

我试图将文本数组的每个成员显示几秒钟。我编写的代码始终显示最后一个成员。这应该如何解决?

import SwiftUI

class ViewController: UIViewController {

    var connectedView: UIView?
    var emotionView: UIView?
    var labelsButton = UIButton(type: .roundedRect)
              
    let labels = ["Apple", "Baby", "Cat", "Dog", "Evening", "Friend", "Go", "Happy", "Independent", "Joyful"]
   
    override func viewDidLoad() {
        super.viewDidLoad()
        layoutForConnected()
    }
  
    @objc func didTouchlabelButton(_ sender: UIButton?) {
        sender?.setTitle("Labels", for: .normal)
        layoutForCharacter()
    }
          
    func layoutForConnected() {
        if connectedView == nil {
            connectedView = UIView(frame: view.bounds)
            connectedView?.backgroundColor = UIColor.white
            
            labelsButton.frame = CGRect(x: 70, y: 475, width: 180, height: 60)
            labelsButton.setTitle("Labels", for: .normal)
            labelsButton.addTarget(self,action:
                                        #selector(didTouchlabelButton),for: .touchUpInside)
            labelsButton.tintColor = UIColor.black
            labelsButton.backgroundColor = UIColor.init(hue: 0.563, saturation: 1.0, brightness: 0.93, alpha: 1.0)
            labelsButton.layer.cornerRadius = 20
            connectedView?.addSubview(labelsButton)
        }
        
        if let connectedView = connectedView {
            self.view.addSubview(connectedView)
        }
    }
    
    var displayTime = 5.0
    var index = 0
       
    func layoutForCharacter() {
                
        // set up frame and background color
        emotionView = UIView(frame: view.bounds)
        emotionView?.backgroundColor = UIColor.init(hue: 0.563, saturation: 1.0, brightness: 0.93, alpha: 1.0)
        
        let labelLabel = UILabel(frame: CGRect(x: 0, y: 400 , width: view.frame.size.width, height: 20))
        labelLabel.textAlignment = .center
        
        // Display each label for displayTime seconds
               index = 0
        
        view?.addSubview(labelLabel)
        labelLabel.text = labels[index]
        
        while index < 9
        {
          labelLabel.text = labels[index] // Displays "Joyful", i.e. index = 9
            
            DispatchQueue.main.asyncAfter(deadline: .now() + displayTime * Double(index))
            {  [self] in
                labelLabel.text = labels[index]
            }
            self.index += 1
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + displayTime * Double(index))
        {  [self] in
            self.layoutForConnected()
        }
    }
}

我已经更改了

DispatchQueue.main.asyncAfter
的位置,但这并没有解决问题。

arrays swift delay display
1个回答
0
投票

我认为你可以用递归的方式重写它。很容易阅读。

private func recursiveShowUpEmotion() {
    guard index < 9 else {
        layoutForConnected()
        return
    }
    emotionLabel?.text = labels[index]
    
    DispatchQueue.main.asyncAfter(deadline: .now() + displayTime) {
        self.index += 1
        self.recursiveShowUpEmotion()
    }
}

在您的场景中,您延迟了 label.text 分配,而不是索引。所以当循环执行时,

index
将立即达到9。然后里面10次
DispatchQueue
.

© www.soinside.com 2019 - 2024. All rights reserved.