Loop scrolling of UIscrollView and UIPageControl and uipagecontrol

Source: Internet
Author: User

Loop scrolling of UIscrollView and UIPageControl and uipagecontrol

 

Because I have been searching for images on the Internet for a long time yesterday, many of them can only scroll to the right, and one image and one imageview have a terrible workload. The example below is no matter how many images you have, only a few values in my code are related. You only need to modify the maximum values of paging and cyclic I. Of course, you 'd better change the image name to an ordered one for convenience. It is convenient for you to add to a variable set.

 

 

 

 

 

 

 

 

 

If you have five or more pages, you can jump from the last graph to the first one.

In fact, there are only three images in total, and the images are added in a loop.

Code on

# Import <UIKit/UIKit. h> # define WIDTH self. view. bounds. size. width # define HEIGHT self. view. bounds. size. height @ interface ViewController: UIViewController <UIScrollViewDelegate> @ property (strong, nonatomic) UIScrollView * scrollview; @ property (strong, nonatomic) UIPageControl * pagecontrol; // Store Image @ property (strong, nonatomic) NSMutableArray * imageArray; // the current page number @ property (assign, nonatomic) int currentPage; // store the image @ property (strong, nonatomic) UIImageView * firstImage; @ property (strong, nonatomic) UIImageView * secondImage; @ property (strong, nonatomic) UIImageView * thirdImage; @ end

 

 

# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. scrollview = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 20, WIDTH, HEIGHT)]; self. scrollview. contentSize = CGSizeMake (WIDTH * 3, 0); // whether the page is self. scrollview. pagingEnabled = YES; // Add proxy self. scrollview. delegate = self; // hide the scroll bar self. scrollview. showsHorizontalScrollIndicator = NO; [self. view addSubview: self. scrollview]; self. pagecontrol = [[UIPageControl alloc] initWithFrame: CGRectMake (WIDTH/5*3, HEIGHT/5*4, WIDTH/3, 40)]; // set the current page self. pagecontrol. currentPage = 0; // page self. pagecontrol. numberOfPages = 5; // specify the page color self. pagecontrol. currentPageIndicatorTintColor = [UIColor redColor]; self. pagecontrol. pageIndicatorTintColor = [UIColor blueColor]; [self. view addSubview: self. pagecontrol]; // initialize the set of stored images self. imageArray = [NSMutableArray array]; for (int I = 1; I <6; I ++) {UIImage * image = [UIImage imageNamed: [NSString stringWithFormat: @ "% d", I]; [self. imageArray addObject: image];} self. firstImage = [[UIImageView alloc] init]; self. secondImage = [[UIImageView alloc] init]; self. thirdImage = [[UIImageView alloc] init]; // the current page number self. currentPage = 0; [self reloadImage];}-(void) reloadImage {// first case, first page if (self. currentPage = 0) {self. firstImage. image = [self. imageArray lastObject]; self. secondImage. image = [self. imageArray objectAtIndex: self. currentPage]; self. thirdImage. image = [self. imageArray objectAtIndex: self. currentPage + 1];} // The last page of else if (self. currentPage = self. imageArray. count-1) {self. firstImage. image = [self. imageArray objectAtIndex: self. currentPage-1]; self. secondImage. image = [self. imageArray objectAtIndex: self. currentPage]; self. thirdImage. image = [self. imageArray objectAtIndex: 0];} // third case intermediate page else {self. firstImage. image = [self. imageArray objectAtIndex: self. currentPage-1]; self. secondImage. image = [self. imageArray objectAtIndex: self. currentPage]; self. thirdImage. image = [self. imageArray objectAtIndex: self. currentPage + 1];} self. firstImage. frame = CGRectMake (0, 0, WIDTH, HEIGHT); self. secondImage. frame = CGRectMake (WIDTH, 0, WIDTH, HEIGHT); self. thirdImage. frame = CGRectMake (WIDTH * 2, 0, WIDTH, HEIGHT); [self. scrollview addSubview: self. firstImage]; [self. scrollview addSubview: self. secondImage]; [self. scrollview addSubview: self. thirdImage]; self. scrollview. contentOffset = CGPointMake (WIDTH, 0);}-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {float x = self. scrollview. contentOffset. x; // left if (x <0 | x = 0) {if (self. currentPage = 0) {self. currentPage = (int) self. imageArray. count-1;} else {self. currentPage --;} // to the right if (x> WIDTH * 2 | x = WIDTH * 2) {if (self. currentPage = (int) self. imageArray. count-1) {self. currentPage = 0;} else {self. currentPage ++;} self. pagecontrol. currentPage = self. currentPage; [self reloadImage];}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ 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.