如何基于计时器创建“ if”语句

时间:2020-04-17 22:01:36

标签: swift if-statement timer collectionview

我的ViewController中有一个Timer以及一个集合视图。

我想根据影响收集视图中某些内容的计时器运行if语句。

但是,当我将语句与单元格一起放入集合视图时,它不会运行。就像计时器对象是静态的一样。 我尝试将计时器放入集合视图中,但是这引发了一个错误:“计时器必须是顶级对象,而不是本地对象。”

这是我的代码:

@objc func timerElapsed() {
        seconds -= 1
        timerLabel.text = "\(seconds)"
        if seconds <= 0 {
            timer?.invalidate()
        }
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellImages.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! CollectionViewCell
        cell.yellowSquare.image = cellImages[indexPath.item]
        return cell
    }

0 个答案:

没有答案