栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

iOS scrollview实现三屏复用循环广告

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

iOS scrollview实现三屏复用循环广告

循环广告我们在开发中已经是熟得不能再熟了,今天整理这篇scrollview三屏复用广告。

原理使用scrollview里的三个imageview分别去加载不同的图片,用少量的资源来显示大量或不确定的广告数量,不然如果用普通方法实现广告,难道10个广告用12个scrollview的contentsize去做,岂不是太浪费资源了

代码如下,实现所有数量的循环广告,当广告只有一个时,仅采用单图显示,>=2个广告时,自动采用三屏复用

这里添加图片的方式是通过网络请求,更新服务器上的广告,如果仅使用本地广告,可以将.m文件里的全部图片的添加方式

如:

self.endImageView.image = self.imageArray[endImageCount];

修改为

self.endImageView.image = [UIImage imageNamed:self.imageArray[endImageCount]];

然后在使用该类时,直接将本地图片的名字用数组传过去就行了,如
cview.imageArray = [[NSMutableArray alloc]initWithObjects:@"图片1",@"图片2",@"图片3", nil];

或者不改则使用方法如

NSArray *imageArr = [[NSArray alloc]initWithObjects:@"banner_理财.jpg",@"banner_惠普",@"banner_炒股", nil];

  for (int i=0; i<3; i++) {

    UIImage *cirImage1 = [UIImage imageNamed:imageArr[i]];

    [cirScrollView.imageArray addObject:cirImage1];

  }

如果图片给的是地址那可以用imageWithURL这个方法来获取图片。

下面讲从服务器获取的广告方式,请求服务器图片及解析这里就不讲了,仅从获取到的data数据后开始。

先新建一个类继承UIView。

.h文件

#import 

@interface CirculateScrollview : UIView

@property (nonatomic,strong)NSMutableArray *imageArray;//图片数组
@property (nonatomic,strong)UIScrollView *circulateScrollView;//广告






@end

.m文件

实现方法是这三个成员变量,用来读取传输过来的图片在数组中的位置,三屏复用里,我们显示的位置是scrollview的中间位置,左边广告是全部广告的最后一个,中间显示第一个,右边的显示第二个,然后根据左滑成员变量递增,当变量递增到超过广告总数时,重新赋值第一个广告,而右滑递减,递减至-1时,即不在数组范围时,重新赋值广告数组的最后一个
#import "CirculateScrollview.h"

#define ViewWidth self.frame.size.width
#define ViewHeight self.frame.size.height
#define AllImageCount self.imageArray.count-1

@interface CirculateScrollview()
{
  NSInteger endImageCount;//左边图片
  NSInteger oneImageCount;//中间图片[当前看到的图片]
  NSInteger secondImageCount;//右边图片
}
@property (nonatomic,strong)UIImageView *endImageView;
@property (nonatomic,strong)UIImageView *oneImageView;
@property (nonatomic,strong)UIImageView *secondImageView;
@property (nonatomic,strong)UIPageControl *pageCtl;

@end

@implementation CirculateScrollview


-(id)initWithframe:(CGRect)frame
{
  self = [super initWithframe:frame];
  if (self) {
    
  }
  return self;
}

-(NSMutableArray *)imageArray
{
  if (!_imageArray) {
    _imageArray = [[NSMutableArray alloc]init];
  }
  return _imageArray;
}

- (void)drawRect:(CGRect)rect {
  self.circulateScrollView = [[UIScrollView alloc]initWithframe:CGRectMake(0, 0, ViewWidth, ViewHeight)];
  
  endImageCount = self.imageArray.count-1;
  oneImageCount = 0;
  secondImageCount = 1;
  
  self.circulateScrollView.showsHorizontalScrollIndicator = NO;
  self.circulateScrollView.pagingEnabled = YES;
  self.circulateScrollView.delegate = self;
  self.circulateScrollView.bounces = NO;
  
  self.circulateScrollView.contentOffset = CGPointMake(ViewWidth, 0);
  
  self.backgroundColor = [UIColor whiteColor];
  
  if (!self.imageArray.count) {
    NSLog(@"图片数组为空");
    return;
  }
  
  
  //若广告数量少于2张则不采用三屏复用技术
  if (self.imageArray.count<=1){
    self.circulateScrollView.contentSize = CGSizeMake(ViewWidth, ViewHeight);
    
    self.endImageView = [[UIImageView alloc]initWithframe:CGRectMake(0, 0, ViewWidth, ViewHeight)];
    self.endImageView.image = self.imageArray[endImageCount];
    [self.circulateScrollView addSubview:self.endImageView];
    [self addSubview:self.circulateScrollView];
    
  }else{
    self.circulateScrollView.contentSize = CGSizeMake(ViewWidth*3, ViewHeight);
    
    //左
    self.endImageView = [[UIImageView alloc]initWithframe:CGRectMake(0, 0, ViewWidth, ViewHeight)];
    self.endImageView.image = self.imageArray[endImageCount];
    [self.circulateScrollView addSubview:self.endImageView];
    //中
    self.oneImageView = [[UIImageView alloc]initWithframe:CGRectMake(ViewWidth, 0, ViewWidth, ViewHeight)];
    self.oneImageView.image = self.imageArray[oneImageCount];
    [self.circulateScrollView addSubview:self.oneImageView];
    //右
    self.secondImageView = [[UIImageView alloc]initWithframe:CGRectMake(ViewWidth*2, 0, ViewWidth, ViewHeight)];
    self.secondImageView.image = self.imageArray[secondImageCount];
    [self.circulateScrollView addSubview:self.secondImageView];
    
    
    [self addSubview:self.circulateScrollView];
    [self pageNumControl];
  }

}
//添加页符
-(void)pageNumControl
{
  self.pageCtl = [[UIPageControl alloc]initWithframe:CGRectMake(0, ViewHeight-20, ViewWidth, 20)];
  self.pageCtl.backgroundColor = [UIColor lightGrayColor];
  self.pageCtl.currentPageIndicatorTintColor = [UIColor greenColor];
  self.pageCtl.pageIndicatorTintColor = [UIColor whiteColor];
  self.pageCtl.alpha = 0.7;
  self.pageCtl.numberOfPages = AllImageCount+1;
  [self addSubview:self.pageCtl];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  if (scrollView.contentOffset.x == 0) {
    endImageCount--;
    oneImageCount--;
    secondImageCount--;
    if (endImageCount<0) {
      endImageCount = AllImageCount;
    }else if (oneImageCount<0){
      oneImageCount = AllImageCount;
    }
    //适配2张图片
    if (secondImageCount<0){
      secondImageCount = AllImageCount;
    }
    //NSLog(@"endImageCount=%ld oneImageCount=%ld secondImageCount=%ld",endImageCount,oneImageCount,secondImageCount);
    
  }else if(scrollView.contentOffset.x == ViewWidth*2){
    endImageCount++;
    oneImageCount++;
    secondImageCount++;
    if (endImageCount>AllImageCount) {
      endImageCount = 0;
    }else if (oneImageCount>AllImageCount){
      oneImageCount = 0;
    }
    //适配2张图片
    if (secondImageCount>AllImageCount){
      secondImageCount = 0;
    }
  }
  //重新加载显示当前位置的图片
  scrollView.contentOffset = CGPointMake(ViewWidth, 0);
  self.endImageView.image = self.imageArray[endImageCount];
  self.oneImageView.image = self.imageArray[oneImageCount];
  self.secondImageView.image = self.imageArray[secondImageCount];
  self.pageCtl.currentPage = oneImageCount;
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/166885.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号