iOS 广告轮播图的实现

通常情况下我们的APP都会有广告轮播图,今天就给大家带来一个简单的实现广告轮播图的第三方库

首先,我们要导入第三方库的头文件:

#import "CycleScrollView.h"

导入完以后,就是实现代码:

NSMutableArray *viewArray 和NSMutableArray *imagesURLStrings 的作用,

imagesURLStrings,通常情况存储的不是图片链接地址,而是一个Model,这个Model可能有很多属性,例如:图片id、图片链接等,而我们的轮播图类,所需要的只有图片链接,所以这个时候我们就需要一个新的数据源,传递给CycleScrollView这个数据源就是viewArray,这个数组就只存储图片链接,他传递到CycleScrollView

    CycleScrollView *cycleScrollView = [[CycleScrollViewalloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_WIDTH*0.4)animationDuration:5.0];

    [_bannerViewaddSubview: cycleScrollView];

    NSMutableArray *viewArray = [[NSMutableArrayalloc]init];

    NSMutableArray *imagesURLStrings =[[NSMutableArra alloc]initWithObjects:@"http://images.*****.vip/xinglixiang.png",@"http://images.******.vip/jiaxiaolianmeng95.jpg",@"http://images.******.vip/youhzengliancheng2.png",@"http://images.******.vip/longbao2.png",@"http://images.******.vip/lunbotu3.png",nil];

    for (int i =0; i < imagesURLStrings.count; i++) {//将获取的轮播图图片放到控件上

        UIImageView *tempImageView =IMAGE_VIEW(0,0,SCREEN_WIDTH,SCREEN_WIDTH*0.4);

        //将网络图片通过第三方实现显示,如果图片不显示时图片地址问题

        [tempImageView setImageWithURL:[NSURLURLWithString:imagesURLStrings[i]]placeholderImage:BANNER_PLACEHOLDERIMAGE];

        tempImageView.contentMode = UIViewContentModeScaleAspectFill;//设置图片显示属性

        tempImageView.clipsToBounds =YES;

        [viewArray addObject:tempImageView];//在将imageview放倒数组里

    }

    [cycleScrollView setFetchContentViewAtIndex:^UIView *(NSInteger(pageIndex)) {

        return [viewArrayobjectAtIndex:pageIndex];//返回数组的坐标的内容

    }];

    [cycleScrollView setTotalPagesCount:^NSInteger{

        return viewArray.count;//返回数据源的个数

    }];

    [cycleScrollView setTapActionBlock:^(NSInteger(pageIndex)) {

        //点击的第几个图片

    }];

以上就是实现代码,非常简单, cycleScrollView的第三方库链接如下:

csdn:http://download.csdn.net/download/wangqinglei0307/10138760

GitHub:https://github.com/WangQingLei307909/CycleScrollView-WQL

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

王 哪跑!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值