在“集合”视图单元上滑动页面视图控制器

时间:2019-07-07 13:29:52

标签: swift xcode

我有一个主视图控制器,它具有一个集合视图。我只想在单元格单击上滑动页面。

HomeViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderViewCell", for: indexPath) as! HeaderViewCell
        cell.menuImg.image = menu[indexPath.row].menuImg
        cell.menuTitle.text = menu[indexPath.row].title
        if cell.isSelected ==  true{
            cell.tintView.backgroundColor = UIColor.green
            cell.backgroundColor = UIColor.green
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let height = 80
        if (indexPath.row == 2){
            return CGSize(width: 150, height: height) }
        else if (indexPath.row == 3) || (indexPath.row == 4) {
            return CGSize(width: 110, height: height)}
        return CGSize(width: 80, height:height)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let indexPath1 = IndexPath(row: indexPath.row, section: 0)
       if  var cell = CollectionView.cellForItem(at: indexPath1) as? HeaderViewCell
            {
                cell.tintView.backgroundColor = UIColor.green
            }
        }
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let indexPath1 = IndexPath(row: indexPath.row, section: 0)
        if  var cell = CollectionView.cellForItem(at: indexPath1) as? HeaderViewCell
        {
            UIView.animate(withDuration: 0.3) {
                cell.tintView.backgroundColor = UIColor.black
            }

        }
    }

}



// This is my page view controller.....******



import UIKit

class IntroPages: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    var pages = [UIViewController]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.dataSource = self

        let p1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "PollViewController")
        let p2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "AllViewController")

        pages.append(p1)
        pages.append(p2)

        setViewControllers([p1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)


    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController)-> UIViewController? {

        let cur = pages.index(of: viewController)!

        // if you prefer to NOT scroll circularly, simply add here:
        // if cur == 0 { return nil }

        let prev = abs((cur - 1) % pages.count)
        return pages[prev]

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController)-> UIViewController? {

        let cur = pages.index(of: viewController)!

        // if you prefer to NOT scroll circularly, simply add here:
        // if cur == (pages.count - 1) { return nil }

        let nxt = abs((cur + 1) % pages.count)
        return pages[nxt]
    }

    func presentationIndex(for pageViewController: UIPageViewController)-> Int {
        return pages.count
    }
}

0 个答案:

没有答案