简体   繁体   中英

CollectionView highlight to next cell automatically every 3 second swift 2

I am trying to create horizontal slider like BigBasket.com. I am using collectionview for paging. I am succeed in scrolling items horizontally, automatic and manually both. but I am not getting how to highlight next cell one by one every 3 second.Please help me to do this.

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
}

So, what you want is to highlight a cell one by one after 3 sec right?

Here is a peace of code that might help.

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

Let us know if this work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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