【问题标题】:xcode swift 4 number increase animation? [duplicate]xcode swift 4数字增加动画? [复制]
【发布时间】:2018-05-22 09:10:16
【问题描述】:

我正在尝试为 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


【解决方案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
    }
}

【讨论】:

  • 谢谢,但它应该像奖励硬币一样快
  • 只需将 Timer.scheduledTimer(timeInterval: 1, Time 减少到 0.15 而不是 1 或更多,因为您希望更改文本值的速度更快
  • 更新值取决于计时器回调减少计时器时间和值将以更快的速度更新
【解决方案2】:

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

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

// 或

 let timer  = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { (t) in

        self.lbl_counter.text =  "\(self.lbl_counter.tag)"

        lbl_counter.tag += 1

        if lbl_counter.tag == 501 {

            t.invalidate()
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2012-06-19
    相关资源
    最近更新 更多