iOS利用UIPageViewController实现图片轮播图

8 篇文章 0 订阅
iOS利用UIPageViewController实现图片轮播图。

UIPageViewController不紧可以实现翻页效果,也可以实现水平滚动效果,用这个控制器实现轮播图效果,逻辑简单,而且是用系统自带的控制器实现,没有复杂的逻辑处理,如果用UIScrollView实现就会考虑复用,切换逻辑等等。

1,UIPageViewController 的UIPageViewControllerTransitionStyleScroll模式就是水平滚动模式。
2,UIPageViewController结合自定义视图,封装到一个自定义视图中YXCircleScrollImageView。
3,可以加载网络图片,网络图片带缓存。
4,使用简单:
    CGRect frame = CGRectZero;
    frame.size = self.localImageCircleView.frame.size;
    __weak typeof(self) weakSelf = self;
    YXCircleScrollImageView * view = [YXCircleScrollImageView circleScrollImageViewWithFrame:frame ImageNumber:^NSInteger(YXCircleScrollImageView *view) {
        return 6;   //图片数量
    } ImageOrUrlAtIndex:^id(NSInteger index) {   //图片或者图片URL
        NSString * imageName = [@"inbar" stringByAppendingFormat:@"%ld_02",index];
        return [UIImage imageNamed:imageName];
    } ImageDefaultAtIndex:^UIImage *(NSInteger index) {  //默认图片
        return nil;
    } SingleTapHandle:^(NSInteger index) {   //图片点击回调
        [weakSelf localCircleScorllImageViewClickAtIndex:index];
    }];

    [self.localImageCircleView addSubview:view];

5,效果图

源码地址:点击打开链接 (可用cocoapods加入项目)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `UICollectionView` 或 `UIPageViewController` 来实现 iOS 图片轮播。 使用 `UICollectionView` 实现的方法如下: 1. 在您的视控制器中,创建一个 `UICollectionView` 实例,并将其作为子视添加到您的视控制器的视中。 2. 使用自定义布局实现循环滚动。可以参考以下代码: ``` class LoopCollectionViewFlowLayout: UICollectionViewFlowLayout { override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { guard let collectionView = collectionView else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) } let collectionViewSize = collectionView.bounds.size let proposedContentOffsetCenterX = proposedContentOffset.x + collectionViewSize.width * 0.5 let proposedRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionViewSize.width, height: collectionViewSize.height) guard let layoutAttributes = layoutAttributesForElements(in: proposedRect) else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) } let centerX = proposedContentOffsetCenterX let offset = CGPoint(x: proposedContentOffset.x + nearestTargetOffset(for: layoutAttributes, with: centerX), y: proposedContentOffset.y) return offset } private func nearestTargetOffset(for layoutAttributes: [UICollectionViewLayoutAttributes], with centerX: CGFloat) -> CGFloat { let targetAttributes = layoutAttributes.sorted { abs($0.center.x - centerX) < abs($1.center.x - centerX) } let nearestAttribute = targetAttributes.first return nearestAttribute?.center.x ?? 0 - centerX } } ``` 3. 创建自定义 `UICollectionViewCell` 类,并在其中添加一个 `UIImageView` 用于显示片。 4. 实现 `UICollectionViewDataSource` 协议中的方法,用于设置片数据源和自定义的 `UICollectionViewCell`。 5. 实现定时器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值