如何在for循环中进行定时活动Swift

时间:2016-03-04 21:17:06

标签: ios swift

我到处寻找并且没有找到满足我需求的答案,我需要一些方法,这样当我按下按钮时,系列的每个单元格都会被漆成黑色。

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource {

    @IBOutlet weak var collection: UICollectionView!

    @IBAction func startButton(sender: AnyObject) {

        for item in 0...24 {
            var place = NSIndexPath(forItem: item, inSection: 0)

            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "paintCell:", userInfo: "place", repeats: false)

        }

    }

    func paintCell (cell: NSIndexPath) {

        collection.cellForItemAtIndexPath(cell)?.contentView.backgroundColor = UIColor.blackColor()

    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 25
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("1", forIndexPath: indexPath)
        cell.contentView.backgroundColor = UIColor.whiteColor()
        return cell

}

    var timer = NSTimer()

}

2 个答案:

答案 0 :(得分:0)

paintCell的签名是错误的。 NSTimer的目标调用应该以{{1​​}}作为唯一的输入参数:

NSTimer

但为什么要求25个计时器才能将25个细胞变为黑色?这些计时器将在同一时间点火。如果你只使用@IBAction func startButton(sender: AnyObject) { for i in 0..<25 { let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: i, inSection: 0)) NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("paintCell:"), userInfo: cell, repeats: false) } } func paintCell(timer: NSTimer) { if let cell = timer.userInfo as? UICollectionViewCell { cell.contentView.backgroundColor = UIColor.blackColor() } timer.invalidate() } 循环,效率会更高:

for

很好,每个人都很容易阅读!

另一方面,如果您希望将每个单元格逐个黑色 1秒,请使用for i in 0..<25 { let indexPath = NSIndexPath(forItem: index, inSection: 0) let cell = self.collectionView.cellForItemAtIndexPath(indexPath) cell?.contentView.backgroundColor = UIColor.blackColor() }

dispatch_after

答案 1 :(得分:-1)

好的 - 第一个问题是您已将选择器定义为paintCell,但您没有与之匹配的功能 - 您有paintCell(cell: NSIndexPath) - 这就是您无法识别选择器的原因

那就是说 - 它仍然不是解决这个问题的最好办法。您不需要25个不同的计时器。如果您的代码正常工作,则所有25个计时器将同时触发,并更新图像。如果这是你真正想要的,你只需要一个计时器,一次点火。如果你真的希望它们每秒更换一次直到完成所有操作,那么这里有一个不同的方法,使用单个计时器和索引的变量。

class ViewController: UIViewController, UICollectionViewDataSource
{
    @IBOutlet weak var collection: UICollectionView!

    var indexCollection = 0
    var timer = NSTimer()

    @IBAction func startButton(sender: AnyObject)
    {
        // start the timer running, and reset the index
        indexCollection = 0
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "paintCell", userInfo: nil, repeats: true)
    }

    func paintCell()
    {
        print(indexCollection)  // just for demonstration.  You don't need this

        // access the cell through the indexCollection
        collection.cellForItemAtIndexPath(NSIndexPath(forItem: indexCollection, inSection: 0))?.contentView.backgroundColor = UIColor.blackColor()

        if indexCollection < 24
        {
            indexCollection++
        }
        else
        {
            // everything is finished, so stop the timer
            timer.invalidate()
        }
    }