【问题标题】:How to add PageviewController inside collectionview如何在 collectionview 中添加 PageviewController
【发布时间】:2015-08-25 09:28:13
【问题描述】:

我在UIViewController 中有一个UICollectionView。现在,我需要在我创建的自定义UICollectionViewCell 中添加一个UIPageViewController。我该怎么做?

这是我的方法。

UIVIewController.h

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        PViewCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

        [cell.pageViewController addTarget:self action:@selector(changed:) forControlEvents:UIControlEventTouchUpInside];

        return cell;

}

PViewCollectionViewCell.h

#import <UIKit/UIKit.h>



@interface PViewCollectionViewCell : UICollectionViewCell <UIPageViewControllerDataSource>

@property (strong, nonatomic) IBOutlet UIImageView *pageBannerImageView;

@property (strong, nonatomic) IBOutlet UIPageControl *pageViewController;

- (IBAction)pageVIewChanged:(id)sender;



@end

【问题讨论】:

  • 你想要哪个,UIPageViewController 或 UIPageControl - 它们是非常不同的东西。
  • 我在找UIPageViewController

标签: ios objective-c


【解决方案1】:

您在问“如何实现UIPageViewController”并使用UIPageControl 给出代码示例。这是完全不同的事情。我永远不会推荐在 UICollectionViewCell 上实现 UIPageViewController,因为它可能会影响你的性能。最好的方法是 UIScrollView + UIPageControl。

在你的 UICollectionViewCell 上添加 UIScrollViewDelegate:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

另外,使用以下方法在滚动条上启用分页:

self.scrollView.pagingEnabled = YES;

Here你可以找到很多描述得很好的答案。我想说的是,你在哪里实现分页并不重要——在 UIView 或 UICollectionViewCell 上。您唯一应该做的就是确保 UICollectionView 滚动手势不会取消您的 UICollectionView 分页滚动手势。

请不要重复问题。首先查看现有帖子,只有在没有找到任何内容时才创建自​​己的帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2015-05-23
    • 2020-07-16
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多