UICollectionView水平布局,分页IOS Swift 3.0

时间:2017-04-29 13:29:01

标签: ios objective-c swift3 uicollectionview uicollectionviewlayout

我想显示两个单元格之间的分页和空格的UIcollectionview单元格水平布局。

向左和向右滑动以移动集合视图页。

我在故事板中设置了以下属性。

滚动启用'false',分页启用'true',滚动方向'水平'

enter image description here

为此,我添加了SwipeGesture,请查看以下代码。

   import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource , UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate{

    @IBOutlet weak var pageControl: UIPageControl!;
    @IBOutlet weak var heightOfCollection: NSLayoutConstraint!;
    @IBOutlet weak var MyCollection: UICollectionView!;

    var cellHeight:CGFloat = 0.0;
    var numberOfItems: Float = 10;
    var setCurrentIndex:Int = 0;

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Add Gestures
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)));
        swipeRight.direction = UISwipeGestureRecognizerDirection.right;
        MyCollection.addGestureRecognizer(swipeRight);

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)));
        swipeLeft.direction = UISwipeGestureRecognizerDirection.left;
        MyCollection.addGestureRecognizer(swipeLeft);
    }

    override func viewWillAppear(_ animated: Bool) {

        self.view.layoutIfNeeded();
        cellHeight = (MyCollection.frame.size.width - 20) / 3;
        heightOfCollection.constant = (cellHeight * 2) + 10;
        MyCollection.reloadData();

        MyCollection.clipsToBounds = true;

        let noOfPage = ceil(Double(numberOfItems/6));
        pageControl.numberOfPages = Int(noOfPage.rounded(.up));

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Collection View Delegate Methods.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: cellHeight, height: cellHeight);
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Int(numberOfItems);
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as! MyCell;

        cell.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1.0);

        cell.lblNumber.text = "\(indexPath.row + 1)";

        return cell;
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        print(indexPath.row);
    }

    //MARK:- Handle Swipe Gesture Recognizer.
    func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            case UISwipeGestureRecognizerDirection.right:
                if setCurrentIndex > 0 {
                    setCurrentIndex -= 1

                    let transition:CATransition! = CATransition();
                    transition.duration = 0.5;
                    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut);
                    transition.type = kCATransitionPush;
                    transition.subtype = kCATransitionFromLeft;
                    MyCollection.layer.add(transition, forKey: nil);

                    MyCollection.contentOffset.x = (CGFloat(setCurrentIndex) * MyCollection.frame.size.width) + CGFloat(setCurrentIndex * 10)
                    pageControl.currentPage = setCurrentIndex;
                }


            case UISwipeGestureRecognizerDirection.left:

                if setCurrentIndex < pageControl.numberOfPages - 1 {
                    setCurrentIndex += 1;

                    let transition:CATransition! = CATransition();
                    transition.duration = 0.5;
                    transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut);
                    transition.type = kCATransitionPush;
                    transition.subtype = kCATransitionFromRight;
                    MyCollection.layer.add(transition, forKey: nil);

                    MyCollection.contentOffset.x = (CGFloat(setCurrentIndex) * MyCollection.frame.size.width) + CGFloat(setCurrentIndex * 10)
                    pageControl.currentPage = setCurrentIndex;
                }
            default:
                break
            }
        }
    }
}

我想显示预期的输出,如红色框中的屏幕截图所示。

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我使用 KSTCollectionViewPageHorizo​​ntalLayout 解决了这个问题。

在github @https://github.com/kelystor/KSTCollectionViewPageHorizontalLayout

上查看此演示
  override func viewWillAppear(_ animated: Bool) {

        self.view.layoutIfNeeded();
        cellHeight = (collectionListing.frame.size.width - 20) / 3;
        collectionHeightConst.constant = (cellHeight * 2) + 10;

        let pageHorizontalLayout = KSTCollectionViewPageHorizontalLayout();
        pageHorizontalLayout.itemSize = CGSize(width: cellHeight, height: cellHeight);
        pageHorizontalLayout.lineSpacing = 10;
        pageHorizontalLayout.interitemSpacing = 10;
        collectionListing.collectionViewLayout = pageHorizontalLayout;

        collectionListing.reloadData();
    }

enter image description here