2016-10-19 1 views
0

BigBasket.com과 같은 가로 슬라이더를 만들려고합니다. 나는 페이징을 위해 collectionview를 사용하고있다. 나는 항목을 가로, 자동 및 수동으로 스크롤하는 데 성공합니다. 하지만 3 초마다 다음 셀을 하나씩 강조하는 방법을 알지 못합니다. 제게이 일을하도록 도와주세요.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 
} 

답변

0

그래서 원하는 것은 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 

이 경우 알려주십시오.

+0

신속하게 코드를 변환하여 시도했습니다. 하지만 작동하지 않습니다. 첫 번째 셀만 강조 표시됩니다. 코드를 업로드했습니다. – user3516627

+0

@ user3516627, * ScrollToNextCell * 메소드에 * self.collectionView! .reloadData() *를 추가하는 것을 잊어 버렸습니다. 그렇기 때문에 반복 매개 변수가 필요하지 않습니다. – PrafulD

+0

예. 그것은 작동합니다. 감사. – user3516627

관련 문제