gpt4 book ai didi

ios - 如何在 collectionview 中添加 PageviewController

转载 作者:行者123 更新时间:2023-12-01 16:30:08 25 4
gpt4 key购买 nike

我在 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 的代码示例.这是完全不同的事情。我绝不会推荐在 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 分页滚动手势。

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

关于ios - 如何在 collectionview 中添加 PageviewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32200312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com