IOS achieves three-screen reuse of circular advertisements and ios multiplexing of advertisements

Source: Internet
Author: User

IOS achieves three-screen reuse of circular advertisements and ios multiplexing of advertisements

We are no longer familiar with circular advertisements during development. Today we will sort out this scrollview three-screen reuse advertisement.

The principle is to use the three imageviews in scrollview to load different images, and use a small amount of resources to display a large or uncertain number of advertisements. Otherwise, if you use a common method to implement advertisements, isn't it a waste of resources to use the contentsize of 12 scrollviews for 10 advertisements?

The Code is as follows to implement all the number of cyclic advertisements. When there is only one advertisement, only a single graph is used for display. If the number is greater than or equal to two, the three screens are automatically reused.

Create a new class to inherit the UIView,

. H

1 # import <UIKit/UIKit. h> 2 3 @ interface CirculateScrollview: UIView 4 5 @ property (nonatomic, strong) NSMutableArray * imageArray; // Image array 6 @ property (nonatomic, strong) UIScrollView * circulateScrollView; // advertisement 7 8/* 9 3-screen multiplexing advertisement 10 Applicability: Network request or fixed local advertisement image 11 Applicable to All quantity of advertisement, ad> = 2 automatic use of three screens Reuse Technology 12 usage: Example 13 in the Controller to add an Ad 14 15 CirculateScrollview * cview = [[CirculateScrollview alloc] initWithFrame: CGRectMake (0, 20,320,200)]; 16 for (int I = 0; I <3; I ++) {17 UIImage * image = [UIImage imageNamed: @ "travel map 1"]; // upload image name Method 18 // UIImage * image = UIImage imageWithData: data]; // upload data image method 19 [cview. imageArray addObject: image]; 20} 21 [self. view addSubview: cview]; 22 23 */24 25 26/* 27 image conversion NSData Method 28 test availability 29 NSData * data = UIImageJPEGRepresentation (image, 1 ); 30 */31 32 @ end

In the. m file

The implementation method is these three member variables, which are used to read the position of the transmitted image in the array. When the three screens are reused, the position shown is the center position of the scrollview, the advertisement on the left is the last of all advertisements, with the first in the middle and the second in the right. Then, the variable increases progressively according to the variable on the left slide member. When the variable increases to more than the total number of advertisements, the first advertisement is re-assigned, while the right slide declines. When the value is reduced to-1, that is, if the value is not in the array range, the last advertisement array is re-assigned.

1 # import "CirculateScrollview. h "2 3 # define ViewWidth self. frame. size. width 4 # define ViewHeight self. frame. size. height 5 # define AllImageCount self. imageArray. count-1 6 7 @ interface CirculateScrollview () <UIScrollViewDelegate> 8 {9 NSInteger endImageCount; 10 NSInteger oneImageCount; 11 NSInteger secondImageCount; 12} 13 @ property (nonatomic, strong) UIImageView * endImageView; 14 @ property (nonat Omic, strong) UIImageView * oneImageView; 15 @ property (nonatomic, strong) UIImageView * secondImageView; 16 @ property (nonatomic, strong) UIPageControl * pageCtl; 17 18 @ end 19 20 @ implementation CirculateScrollview 21 22 23-(id) initWithFrame :( CGRect) frame 24 {25 self = [super initWithFrame: frame]; 26 if (self) {27 28} 29 return self; 30} 31 32-(NSMutableArray *) imageArray 33 {34 if (! _ ImageArray) {35 _ imageArray = [[NSMutableArray alloc] init]; 36} 37 return _ imageArray; 38} 39 40-(void) drawRect :( CGRect) rect {41 self. circulateScrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 0, ViewWidth, ViewHeight)]; 42 43 endImageCount = self. imageArray. count-1; 44 oneImageCount = 0; 45 secondImageCount = 1; 46 47 self. circulateScrollView. showsHorizontalScrollIndicator = NO; 48 self. circulateScrollView. pagingEnabled = YES; 49 self. circulateScrollView. delegate = self; 50 self. circulateScrollView. bounces = NO; 51 52 self. circulateScrollView. contentOffset = CGPointMake (ViewWidth, 0); 53 54 self. backgroundColor = [UIColor whiteColor]; 55 56 if (! Self. imageArray. count) {57 NSLog (@ "Empty Image array"); 58 return; 59} 60 61 62 // if there are fewer than two advertisements, the three-screen multiplexing technology is not used. 63 if (self. imageArray. count <= 1) {64 self. circulateScrollView. contentSize = CGSizeMake (ViewWidth, ViewHeight); 65 66 self. endImageView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, ViewWidth, ViewHeight)]; 67 self. endImageView. image = self. imageArray [endImageCount]; 68 [self. circulateScrollView addSubview: self. endImageView]; 69 [self addSubview: self. circulateScrollView]; 70 71} else {72 self. circulateScrollView. contentSize = CGSizeMake (ViewWidth * 3, ViewHeight); 73 74 // left 75 self. endImageView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, ViewWidth, ViewHeight)]; 76 self. endImageView. image = self. imageArray [endImageCount]; 77 [self. circulateScrollView addSubview: self. endImageView]; 78 // medium 79 self. oneImageView = [[UIImageView alloc] initWithFrame: CGRectMake (ViewWidth, 0, ViewWidth, ViewHeight)]; 80 self. oneImageView. image = self. imageArray [oneImageCount]; 81 [self. circulateScrollView addSubview: self. oneImageView]; 82 // right 83 self. secondImageView = [[UIImageView alloc] initWithFrame: CGRectMake (ViewWidth * 2, 0, ViewWidth, ViewHeight)]; 84 self. secondImageView. image = self. imageArray [secondImageCount]; 85 [self. circulateScrollView addSubview: self. secondImageView]; 86 87 88 [self addSubview: self. circulateScrollView]; 89 [self pageNumControl]; 90} 91 92} 93 94-(void) pageNumControl 95 {96 self. pageCtl = [[UIPageControl alloc] initWithFrame: CGRectMake (0, ViewHeight-20, ViewWidth, 20)]; 97 self. pageCtl. backgroundColor = [UIColor lightGrayColor]; 98 self. pageCtl. currentPageIndicatorTintColor = [UIColor greenColor]; 99 self. pageCtl. pageIndicatorTintColor = [UIColor whiteColor]; 100 self. pageCtl. alpha = 0.7; 101 self. pageCtl. numberOfPages = AllImageCount + 1; 102 [self addSubview: self. pageCtl]; 103} 104 105-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView106 {107 if (scrollView. contentOffset. x = 0) {108 endImageCount --; 109 oneImageCount --; 110 secondImageCount --; 111 if (endImageCount <0) {112 endImageCount = AllImageCount; 113} else if (oneImageCount <0) {114 oneImageCount = AllImageCount; 115} 116 // adapted to 2 images 117 if (secondImageCount <0) {118 secondImageCount = AllImageCount; 119} 120 // NSLog (@ "endImageCount = % ld oneImageCount = % ld secondImageCount = % ld", endImageCount, oneImageCount, secondImageCount); 121 scrollView. contentOffset = CGPointMake (ViewWidth, 0); 122 self. endImageView. image = self. imageArray [endImageCount]; 123 self. oneImageView. image = self. imageArray [oneImageCount]; 124 self. secondImageView. image = self. imageArray [secondImageCount]; 125} else if (scrollView. contentOffset. x = ViewWidth * 2) {126 endImageCount ++; 127 oneImageCount ++; 128 secondImageCount ++; 129 if (endImageCount> AllImageCount) {130 endImageCount = 0; 131} else if (oneImageCount> AllImageCount) {132 oneImageCount = 0; 133} 134 // adapted to 2 images 135 if (secondImageCount> AllImageCount) {136 secondImageCount = 0; 137} 138 scrollView. contentOffset = CGPointMake (ViewWidth, 0); 139 self. endImageView. image = self. imageArray [endImageCount]; 140 self. secondImageView. image = self. imageArray [secondImageCount]; 141 self. oneImageView. image = self. imageArray [oneImageCount]; 142} 143 self. pageCtl. currentPage = oneImageCount; 144} 145 146 @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.