Comprehensive strengths of iOS-UIScrollView and UIPageControl, scrolling, carousel, and uiscrollview

Source: Internet
Author: User

Comprehensive strengths of iOS-UIScrollView and UIPageControl, scrolling, carousel, and uiscrollview
This code mainly implements switching between images

 

Directory structure

 

Code

ViewController. m file

# Import "ViewController. h "@ interface ViewController () @ property (strong, nonatomic) UIScrollView * scrollView; @ property (strong, nonatomic) UIPageControl * pageControl; // Store Image @ property (strong, nonatomic) UIImageView * firstIamges; @ property (strong, nonatomic) UIImageView * secondImage; @ property (strong, nonatomic) UIImageView * thirdImage; // a set of stored images @ property (strong, nonatomic) NSMutableArray * imageArray; // the current page number @ property (assign, nonatomic) int currentPage; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 20, WIDTH, HEIGHT)]; self. scrollView. contentSize = CGSizeMake (WIDTH * 3, 0); self. scrollView. pagingEnabled = YES; self. scrollView. delegate = self; self. scrollView. showsHorizontalScrollIndicator = NO; [self. view addSubview: self. scrollView]; self. pageControl = [[UIPageControl alloc] initWithFrame: CGRectMake (WIDTH/2.7, 300, WIDTH/5, 20)]; // 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. firstIamges = [[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. firstIamges. 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. firstIamges. image = [self. imageArray objectAtIndex: self. currentPage-1]; self. secondImage. image = [self. imageArray objectAtIndex: self. currentPage]; self. thirdImage. image = [self. imageArray objectAtIndex: 0];} // intermediate page else {self. firstIamges. 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. firstIamges. frame = CGRectMake (0, 0, WIDTH, 300); self. secondImage. frame = CGRectMake (WIDTH, 0, WIDTH, 300); self. thirdImage. frame = CGRectMake (WIDTH * 2, 0, WIDTH, 300); [self. scrollView addSubview: self. firstIamges]; [self. scrollView addSubview: self. secondImage]; [self. scrollView addSubview: self. thirdImage]; self. scrollView. contentOffset = CGPointMake (WIDTH, 0) ;}# pragma mark-Scrollview Delegate // change the chart in the rolling end state-(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

ViewController. h file

#import <UIKit/UIKit.h>#define WIDTH self.view.frame.size.width#define HEIGHT self.view.frame.size.height@interface ViewController : UIViewController<UIScrollViewDelegate>@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.