Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xcode swift 4增加动画数量?_Swift - Fatal编程技术网

xcode swift 4增加动画数量?

xcode swift 4增加动画数量?,swift,Swift,我正在尝试在swift 4中设置UILabel数量增加的动画,我对这种语言还是新手,我正在尝试做的是: for index in 1...500 { self.lbl_counter.text = "\(index)" } 值始终为500 我也试过这个 for index in 1...500 { DispatchQueue.main.async{ self.lbl_counter.text = "\(index)" } } 我在这里错过了什么,使它成为增加数字

我正在尝试在
swift 4
中设置UILabel数量增加的动画,我对这种语言还是新手,我正在尝试做的是:

for index in 1...500 {
   self.lbl_counter.text = "\(index)"
}
值始终为500

我也试过这个

 for index in 1...500 {
DispatchQueue.main.async{
       self.lbl_counter.text = "\(index)"
}
    }
我在这里错过了什么,使它成为增加数字的动画


您当前写入的内容将循环的最后一个数字500分配给标签文本,因为在将数字从0设置到500之间没有延迟

for index in 1...500 {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5 * Double(i) ) {
      self.lbl_counter.text = "\(index)"
    }
}
//或

尝试以下代码

import UIKit

class ViewController: UIViewController
{
    /// Label
    private var customLabel : UILabel?

    /// MAximum Count to which label will be Updated
    private var maxCount : Int?
    /// Count which is currently displayed in Label
    private var currentCount : Int?
    /// Timer To animate label text
    private var updateTimer : Timer?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        customLabel = UILabel()
        customLabel?.textColor = .black

        /// Add label to View
        addConstraints()

        /// Start Timer
        DispatchQueue.main.async {
            self.maxCount = 100
            self.currentCount = 0
            self.updateTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.updateLabel), userInfo: nil, repeats: true)
        }
    }

    @objc func updateLabel() {
        self.customLabel?.text = String(currentCount!)
        currentCount! += 1
        if currentCount! > maxCount! {
            /// Release All Values
            self.updateTimer?.invalidate()
            self.updateTimer = nil
            self.maxCount = nil
            self.currentCount = nil
        }
    }

    func addConstraints(){
        /// Add Required Constraints
        self.view.addSubview(customLabel!)
        customLabel?.translatesAutoresizingMaskIntoConstraints = false
        customLabel?.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 50).isActive = true
        customLabel?.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50).isActive = true
        customLabel?.heightAnchor.constraint(equalToConstant: 50).isActive = true
        customLabel?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
    }
}

您可以使用定时器在特定时间内增加标签值。这可能与主题无关,但我建议您检查:@AhmadF正是我想要的,谢谢谢谢,但它应该像奖赏硬币一样快。请减少定时器。scheduledTimer(时间间隔:1,时间为0.15,而不是1或更多。如果您希望更改文本值,则时间间隔值取决于计时器回调减少计时器时间,值将以更快的速度更新。)
import UIKit

class ViewController: UIViewController
{
    /// Label
    private var customLabel : UILabel?

    /// MAximum Count to which label will be Updated
    private var maxCount : Int?
    /// Count which is currently displayed in Label
    private var currentCount : Int?
    /// Timer To animate label text
    private var updateTimer : Timer?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        customLabel = UILabel()
        customLabel?.textColor = .black

        /// Add label to View
        addConstraints()

        /// Start Timer
        DispatchQueue.main.async {
            self.maxCount = 100
            self.currentCount = 0
            self.updateTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.updateLabel), userInfo: nil, repeats: true)
        }
    }

    @objc func updateLabel() {
        self.customLabel?.text = String(currentCount!)
        currentCount! += 1
        if currentCount! > maxCount! {
            /// Release All Values
            self.updateTimer?.invalidate()
            self.updateTimer = nil
            self.maxCount = nil
            self.currentCount = nil
        }
    }

    func addConstraints(){
        /// Add Required Constraints
        self.view.addSubview(customLabel!)
        customLabel?.translatesAutoresizingMaskIntoConstraints = false
        customLabel?.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 50).isActive = true
        customLabel?.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -50).isActive = true
        customLabel?.heightAnchor.constraint(equalToConstant: 50).isActive = true
        customLabel?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
    }
}