简体   繁体   English

在Timer中运行UIView.animate

[英]Running UIView.animate in Timer

I made an application that counts down every second starting from the number 10. The countdown is animated . 我制作了一个从数字10开始每秒倒数的应用程序。倒数是动画的 Here's the code: 这是代码:

import UIKit

class ViewController: UIViewController {

    var startNumber = 10

    lazy var numberLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = label.font.withSize(160)
        label.textColor = .lightGray
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white

        view.addSubview(numberLabel)

        NSLayoutConstraint.activate([
            numberLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            numberLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
            ])

        _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (time) in

            self.numberLabel.text = String(self.startNumber)
            self.startNumber -= 1

            UIView.animate(withDuration: 0.99, animations: {
                self.numberLabel.alpha = 0
            }, completion: { (true) in
                self.numberLabel.alpha = 1
            })

        }
    }

}

The countdown displays an uneven animation of numbers if both the UIView and Timer interval are equal. 如果UIView和Timer间隔均相等,则倒数显示的数字动画会不均匀。 That's why in the code the UIView interval is set to 0.99 instead of 1 to display an animated countdown for every number. 这就是为什么在代码中将UIView间隔设置为0.99而不是1来显示每个数字的动画倒计时的原因。 The animation still isn't smooth since you can see the numbers "flick" in a millisecond but I assume that's because the interval has been set on 0.99 instead of 1. 动画仍然不流畅,因为您可以在一毫秒内看到数字“ flick”,但我认为这是因为间隔已设置为0.99而不是1。

My questions are: 我的问题是:

1) Why does setting an equal interval on UIView and Timer result in an odd countdown animation? 1)为什么在UIView和Timer上设置相等的间隔会导致倒数动画?

2) Does the countdown count still count down for every second or for 0.99 seconds? 2)倒数还是每秒还是倒数0.99秒?

Change the code to look like that 更改代码看起来像这样

 _ = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (time) in


        self.startNumber -= 1

         self.numberLabel.alpha = 1

        UIView.setAnimationCurve(UIViewAnimationCurve.easeInOut)

        UIView.animate(withDuration: 1, animations: {

            self.numberLabel.text = String(self.startNumber)
            self.numberLabel.alpha = 0
        }, completion: { (true) in

        })

    }

1) Why does setting an equal interval on UIView and Timer result in an odd countdown animation? 1)为什么在UIView和Timer上设置相等的间隔会导致倒数动画?

Ans: When setting equal time, the previous animation will surely overlap with the new one. 回答:当设置相等的时间时,以前的动画肯定会与新的动画重叠。 So better to put a lesser time. 所以最好花更少的时间。 Even less than 0.99 甚至小于0.99

2) Does the countdown count still count down for every second or for 0.99 seconds? 2)倒数还是每秒还是倒数0.99秒?

Ans: Countdown will be triggered in each second regardless of animation timer 回答:无论动画计时器如何,倒计时都会每秒触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM