SoFunction
Updated on 2025-04-09

iOS scrollview realizes three-screen multiplexing circular advertising

We are already so familiar with circular advertising that we can’t be more familiar with in the development of circular advertising. Today we have compiled this scrollview three-screen reuse advertisement.

Principle: Use three imageviews in scrollview to load different images separately, and use a small amount of resources to display a large number of or uncertain ads. Otherwise, if you use the normal method to implement ads, wouldn’t it be too wasteful to use the contentsize of 12 scrollviews for 10 ads to do it?

The code is as follows, to realize all number of circular advertisements. When there is only one advertisement, only a single image is displayed. When >= 2 advertisements, three-screen reuse is automatically used.

The way to add pictures here is to update the advertisements on the server through the network request. If only local advertisements are used, you can add all the pictures in the .m file.

like:

= [endImageCount];

Modified to

= [UIImage imageNamed:[endImageCount]];

Then when using this class, just pass the name of the local image in an array, such as
= [[NSMutableArray alloc]initWithObjects:@"Picture 1",@"Picture 2",@"Picture 3", nil];

Or if you don't change it, use it like

NSArray *imageArr = [[NSArray alloc]initWithObjects:@"banner_financial management.jpg",@"banner_HP",@"banner_stock trading", nil];

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

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

    [ addObject:cirImage1];

  }

If the image is given an address, you can use imageWithURL to get the image.

The following is a discussion of the advertising method obtained from the server. I will not talk about the server's pictures and analysis. I will only start with the data data obtained.

Create a new class to inherit the UIView.

.h file

#import <UIKit/>

@interface CirculateScrollview : UIView

@property (nonatomic,strong)NSMutableArray *imageArray;//Picture array@property (nonatomic,strong)UIScrollView *circulateScrollView;//advertise
/*
  Three-screen reuse advertising
  Scope of application: Network request or fixed local advertising pictures
     All numbers of advertisements are applicable, and three-screen multiplexing technology is automatically used when advertisements >=2
  How to use: Example
  In the controller that needs to add ads
 
  CirculateScrollview *cview = [[CirculateScrollview alloc]initWithFrame:CGRectMake(0, 20, 320, 200)];
  for (int i=0; i<3; i++) {
  UIImage *image = [UIImage imageNamed:@"Travel Picture 1"];//How to pass the image name
  //UIImage *image = UIImage imageWithData:data];//Cross data images to convert the data data images requested by the server into image form and then transmit it
  [ addObject:image];
  }
  [ addSubview:cview];
 
  */


/*
  NSData method for image conversion
  Test available
  NSData * data = UIImageJPEGRepresentation(image, 1);
  */

@end

.m file

The implementation method is these three member variables, which are used to read the position of the transmitted image in the array. In the three-screen multiplexing, the position we display is the middle position of scrollview. The advertisement on the left is the last of all advertisements, the first one is displayed in the middle, and the second one is displayed in the right, and then the member variable is increased according to the left slider. When the variable increases to more than the total number of advertisements, the first advertisement is reassigned, and the right slider decreases, decreasing to -1, that is, when it is not in the range of the array, the last advertisement in the ad array is reassigned.
#import ""

#define ViewWidth 
#define ViewHeight 
#define AllImageCount -1

@interface CirculateScrollview()&lt;UIScrollViewDelegate&gt;
{
  NSInteger endImageCount;//The picture on the left  NSInteger oneImageCount;//The middle picture [the picture you see currently]  NSInteger secondImageCount;//The picture on the right}
@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 {
   = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
  
  endImageCount = -1;
  oneImageCount = 0;
  secondImageCount = 1;
  
   = NO;
   = YES;
   = self;
   = NO;
  
   = CGPointMake(ViewWidth, 0);
  
   = [UIColor whiteColor];
  
  if (!) {
    NSLog(@"The picture array is empty");
    return;
  }
  
  
  //If the number of advertisements is less than 2, the three-screen multiplexing technology will not be used.  if (&lt;=1){
     = CGSizeMake(ViewWidth, ViewHeight);
    
     = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
     = [endImageCount];
    [ addSubview:];
    [self addSubview:];
    
  }else{
     = CGSizeMake(ViewWidth*3, ViewHeight);
    
    //Left     = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)];
     = [endImageCount];
    [ addSubview:];
    //middle     = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth, 0, ViewWidth, ViewHeight)];
     = [oneImageCount];
    [ addSubview:];
    //right     = [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth*2, 0, ViewWidth, ViewHeight)];
     = [secondImageCount];
    [ addSubview:];
    
    
    [self addSubview:];
    [self pageNumControl];
  }

}
//Add page characters-(void)pageNumControl
{
   = [[UIPageControl alloc]initWithFrame:CGRectMake(0, ViewHeight-20, ViewWidth, 20)];
   = [UIColor lightGrayColor];
   = [UIColor greenColor];
   = [UIColor whiteColor];
   = 0.7;
   = AllImageCount+1;
  [self addSubview:];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  if ( == 0) {
    endImageCount--;
    oneImageCount--;
    secondImageCount--;
    if (endImageCount&lt;0) {
      endImageCount = AllImageCount;
    }else if (oneImageCount&lt;0){
      oneImageCount = AllImageCount;
    }
    //Adap 2 pictures    if (secondImageCount&lt;0){
      secondImageCount = AllImageCount;
    }
    //NSLog(@"endImageCount=%ld oneImageCount=%ld secondImageCount=%ld",endImageCount,oneImageCount,secondImageCount);
    
  }else if( == ViewWidth*2){
    endImageCount++;
    oneImageCount++;
    secondImageCount++;
    if (endImageCount&gt;AllImageCount) {
      endImageCount = 0;
    }else if (oneImageCount&gt;AllImageCount){
      oneImageCount = 0;
    }
    //Adap 2 pictures    if (secondImageCount&gt;AllImageCount){
      secondImageCount = 0;
    }
  }
  //Reload the picture showing the current location   = CGPointMake(ViewWidth, 0);
   = [endImageCount];
   = [oneImageCount];
   = [secondImageCount];
   = oneImageCount;
}

@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.