UIPageViewControllerでのページ遷移の際に同時に動くタブを作成して、ビルドしようとしています。
イメージとしてはこちらのような感じです。
現状では
collectionView.datasource = self
のところでエラーが出てしまい、動作の確認ができていません。
解決法等お分かりになる方いらっしゃいましたら教えていただけますか。
Swift
1class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UICollectionViewDelegateFlowLayout { 2 3 4 let pageList = ["first", "second", "third"] 5 6 var collectionView:UICollectionView! 7 var page:Int = 0 8 let labelheight:CGFloat = 60 9 10 required init?(coder aDecoder: NSCoder) { 11 super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) 12 } 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 dataSource = self 17 delegate = self 18 setViewControllers([storyboardId(strID: "first") as! UIViewController], direction: .forward, animated: true, completion: nil) 19 20 21 22 collectionView.dataSource = self // ビルドするとここでエラーが起こってしまいます 23 collectionView.delegate = self 24 25 // collectionViewレイアウト作成 26 CollectionViewLayoutSet() 27 28 // collectionViewその他設定 29 collectionView.showsHorizontalScrollIndicator = false 30 collectionView.showsVerticalScrollIndicator = false 31 collectionView.delaysContentTouches = false 32 33 view.addSubview(collectionView) 34 35 self.view.subviews 36 .filter{ $0.isKind(of: UIScrollView.self) } 37 .forEach{($0 as! UIScrollView).delegate = self } 38 } 39 40 41 func storyboardId(strID: String) -> AnyObject! { 42 return (storyboard?.instantiateViewController(withIdentifier: strID)) 43 } 44 45 // 1ページ進む 46 func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 47 let index = pageList.firstIndex(of: viewController.restorationIdentifier!) 48 if index! < (pageList.count - 1) { 49 return (storyboardId(strID: pageList[index! + 1]) as! UIViewController) 50 } else { 51 return nil 52 } 53 } 54 55 56 // 1ページ戻る 57 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 58 let index = pageList.firstIndex(of: viewController.restorationIdentifier!) 59 if 0 < index! { 60 return (storyboardId(strID: pageList[index! - 1]) as! UIViewController) 61 } else { 62 return nil 63 } 64 } 65 66 // Page Controlを表示する 67 func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { 68 return pageList.count 69 } 70 func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { 71 return 0 72 } 73 74 75 76 77 func CollectionViewLayoutSet(){ 78 let flowLayout = UICollectionViewFlowLayout() 79 flowLayout.minimumInteritemSpacing = 0.0 80 flowLayout.minimumLineSpacing = 0.0 81 flowLayout.scrollDirection = .horizontal 82 flowLayout.itemSize = CGSize(width:viewframewidth / 3 , height:CGFloat(labelheight)) 83 let rec = CGRect(x: 0.0, y:statusBarHeight , width:viewframewidth , height: labelheight) 84 collectionView = UICollectionView(frame: rec, collectionViewLayout: flowLayout) 85 } 86} 87 88extension PageViewController: UICollectionViewDelegate, UICollectionViewDataSource { 89 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 90 return pageList.count 91 } 92 93 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 94 collectionView.register(cellType:tab.self) 95 let cell: tab = collectionView.dequeueReusableCell(with: tab.self, for: indexPath) 96 cell.Set(Text: pageList[indexPath.row]) 97 return cell 98 } 99 100 101} 102 103extension PageViewController { 104 func scrollViewDidScroll(_ scrollView: UIScrollView) { 105 if(scrollView.className == "UICollectionView"){ 106 return 107 } 108 109 let LeftRightJug = viewframewidth - scrollView.contentOffset.x 110 if(LeftRightJug == 0){ 111 return 112 } 113 let Move = scrollView.contentOffset.x / 3 114 let first:Int = LeftRightJug > 0 ? 1 : 0 115 let End:Int = LeftRightJug > 0 ? (pageList.count - 1) : (pageList.count - 2) 116 if(first < page && page < End) { 117 let pageWidth = CGFloat(page - 2) * viewframewidth 118 let TabPageWidth = pageWidth / 3 119 120 collectionView.contentOffset = CGPoint(x: Move + TabPageWidth, y:0) 121 } 122 } 123} 124 125

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/14 08:40
2019/06/14 08:44
2019/06/14 08:47