[英]CollectionView highlight to next cell automatically every 3 second swift 2
我正在嘗試創建像BigBasket.com這樣的水平滑塊。 我正在使用collectionview進行分頁。 我成功地水平,自動和手動滾動了項目。 但是我沒法知道如何每3秒一次高亮顯示下一個單元格。請幫助我做到這一點。
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
讓我們知道這項工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.