CollectionView 每 3 秒自动突出显示到下一个单元格 swift 2

CollectionView highlight to next cell automatically every 3 second swift 2

我正在尝试创建像 BigBasket.com 这样的水平滑块。我正在使用 collectionview 进行分页。我成功地水平、自动和手动滚动项目。但我不知道如何每 3 个一个一个地突出显示下一个单元格 second.Please 帮助我做到这一点。

override func viewDidAppear(animated: Bool) {
    rowToHighlight = 0;
    NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(BannerTagCollectionViewController.ScrollToNextCell), userInfo: nil, repeats: true)
}

func ScrollToNextCell() {
    rowToHighlight += 1
    NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(BannerTagCollectionViewController.ScrollToNextCell), userInfo: nil, repeats: true)
}

// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return bannerTagArray.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : BannerTagCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! BannerTagCollectionViewCell

    if(indexPath.row == self.rowToHighlight){
        cell.backgroundColor = UIColor .whiteColor()
        cell.selectedView.backgroundColor = UIColor .greenColor()
    }
    else{
        cell.backgroundColor = UIColor .lightGrayColor().colorWithAlphaComponent(0.5)
        cell.selectedView.backgroundColor = UIColor .clearColor()
    }

    // Configure the cell
    cell.bannerTagProNameLbl?.text = bannerTagArray[indexPath.row]
    cell.bannerTagProDescLbl?.text="bbbbnn"
    return cell
}

所以,你想要的是在 3 秒后一个一个地突出显示一个单元格吧?

这里是一些可能有用的代码。

SampleController.h

@interface SampleController

//The other properties and methods

@property int rowToHighlight;

@end


SampleController.m

// your all code


-(void)viewDidAppear:(BOOL)animated{
     //your all other code
     self.rowToHighlight = 0;
     [self performSelector:@selector(callMe) withObject:nil afterDelay:3.0];
}

-(void)callMe{
    [self.CollectionView reloadData];
    self.rowToHighlight += 1;
    [self performSelector:@selector(callMe) withObject:nil afterDelay:3.0];   
}

//All your delegates of collectionView

- (UICollectionView *)collectionView:(UICollectionView *)tableView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewCell cell = //your code

  //cell operation

  if(indexPath.row == self.rowToHighlight){
        cell.backgroundColor = [UIColor redColor];
  }
  else{
        cell.backgroundColor = [UIColor clearColor];
  }
}


//Your other code

让我们知道这是否有效。