Scroll Cell of UICollectionView to Left & Right direction by clicking on left & right button action

Viewed 10354

I have requirement to make the UICollectionView Cell to be scroll to next 5 items from array by clicking on "right" button and show previous 5 times by clicking on "left button".

enter image description here

Can anyone help me by giving any suggestion to make it possbile in swift.

below is the code written, To show list on collection view in horizontal scroll.

override func viewDidLoad() {

        super.viewDidLoad()

        self.arrFalconList = ["ic_falcon_1","ic_falcon_2","ic_falcon_3","ic_falcon_4","ic_falcon_5","ic_falcon_6","ic_falcon_7"]
    }



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.arrFalconList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let aFalconCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FalconCollectionViewCell", for: indexPath) as! FalconCollectionViewCell

        aFalconCell.imgFalcon.image = UIImage(named: self.arrFalconList[indexPath.row])
        aFalconCell.lblFalcon.text = self.arrFalconList[indexPath.row]
        aFalconCell.imgFalcon.layer.cornerRadius = ((aFalconCell.imgFalcon.frame.width) / 2)
        aFalconCell.imgFalcon.clipsToBounds = true

        if aFalconCell.isSelected {
            aFalconCell.imgFalcon.layer.borderColor = UIColor.init(red: 241.0/255.0, green: 55.0/255.0, blue: 61.0/255.0, alpha: 1.0).cgColor
            aFalconCell.imgFalcon.layer.borderWidth = 2
        }else{
            aFalconCell.imgFalcon.layer.borderColor = UIColor.clear.cgColor
            aFalconCell.imgFalcon.layer.borderWidth = 0
        }


        return aFalconCell

    }

Please share your answer to handle the "left" & "right" button click event.

3 Answers

akash 's answer in Objective-C, except I have made one slight change: I needed to scroll one cell at a time instead of scrolling all the way to the left or right. If you need that functionality change out

float cellWidth = self.collectionView.visibleCells[0].bounds.size.width;

with

float collectionViewWidth = self.collectionView.bounds;

- (IBAction)scrollLeftAction:(id)sender
{
    float cellWidth = self.collectionView.visibleCells[0].bounds.size.width;
    float contentOffset = (float)(floor(self.collectionView.contentOffset.x - cellWidth));
    [self moveCollectionViewToFrame:contentOffset];
}

- (IBAction)scrollRightAction:(id)sender
{
    float cellWidth = self.collectionView.visibleCells[0].bounds.size.width;
    float contentOffset = (float)(floor(self.collectionView.contentOffset.x + cellWidth));
    [self moveCollectionViewToFrame:contentOffset];
}

- (void)moveCollectionViewToFrame:(CGFloat)contentOffset
{
    CGRect frame = CGRectMake(contentOffset, self.collectionView.contentOffset.y, self.collectionView.frame.size.width, self.collectionView.frame.size.height);
    [self.collectionView scrollRectToVisible:frame
                                    animated:YES];
}
Related