IOS-UIScrollView and UIPageControl, iosuipagecontrol
This Code allows you to switch between images by sliding at a fixed position.
The directory is shown as follows:
ViewController. h
# Import <UIKit/UIKit. h> // use a macro to define the WIDTH and height # define WIDTH self. view. frame. size. width # define HEIGHT self. view. frame. size. height @ interface ViewController: UIViewController <UIScrollViewDelegate> @ property (strong, nonatomic) UIScrollView * myScrollView; @ property (strong, nonatomic) UIPageControl * myPageControl; // set of stored images @ property (strong, nonatomic) NSMutableArray * imageArray; // Save the photo @ property (strong, nonatomic) UIImageView * firstimage; @ property (strong, nonatomic) UIImageView * secondimage; @ property (strong, nonatomic) UIImageView * thirimage; // the current page number @ property (assign, nonatomic) int currentPage; @ end
ViewController. m
1 # import "ViewController. h "2 3 @ interface ViewController () 4 5 @ end 6 7 @ implementation ViewController 8 9-(void) viewDidLoad {10 [super viewDidLoad]; 11 self. myScrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 0, WIDTH, HEIGHT)]; 12 self. myScrollView. backgroundColor = [UIColor colorWithRed: 0.315 green: 0.843 blue: 0.892 alpha: 1.000]; 13 self. myScrollView. contentSize = CGSizeMake (3 * WIDTH, HEIGHT); 14 // set page 15 self. myScrollView. pagingEnabled = YES; 16 // hide the scroll bar 17 self. myScrollView. showsHorizontalScrollIndicator = NO; 18 // lock the scroll direction 19 self. myScrollView. directionalLockEnabled = YES; 20 // reference proxy 21 self. myScrollView. delegate = self; 22 23 [self. view addSubview: self. myScrollView]; 24 25 self. myPageControl = [[UIPageControl alloc] init]; 26 CGSize PageSize = CGSizeMake (120, 40); 27 self. myPageControl. frame = CGRectMake (WIDTH-PageSize.width)/2, HEIGHT-PageSize.height-40, PageSize. width, PageSize. height); 28 self. myPageControl. backgroundColor = [UIColor clearColor]; 29 30 // set the current page 31 self. myPageControl. currentPage = 0; 32 // page 33 self. myPageControl. numberOfPages = 4; 34 35 [self. view addSubview: self. myPageControl]; 36 37 // initialize the set of stored images 38 self. imageArray = [NSMutableArray arrayWithCapacity: 1]; 39 for (int I = 1; I <5; I ++) {40 UIImage * image = [UIImage imageNamed: [NSString stringWithFormat: @ "% d", I]; 41 [self. imageArray addObject: image]; 42} 43 44 self. firstimage = [[UIImageView alloc] init]; 45 self. secondimage = [[UIImageView alloc] init]; 46 self. thirimage = [[UIImageView alloc] init]; 47 // the current page number is 48 self. currentPage = 0; 49 50 [self reloadImage]; 51 52} 53 54-(void) reloadImage 55 {56 // first case, page 57 if (self. currentPage = 0) {58 self. firstimage. image = [self. imageArray lastObject]; 59 self. secondimage. image = [self. imageArray objectAtIndex: self. currentPage]; 60 self. thirimage. image = [self. imageArray objectAtIndex: self. currentPage + 1]; 61 62} 63 64 // the second case, the last page 65 else if (self. currentPage = self. imageArray. count-1) {66 self. firstimage. image = [self. imageArray objectAtIndex: self. currentPage-1]; 67 self. secondimage. image = [self. imageArray objectAtIndex: self. currentPage]; 68 self. thirimage. image = [self. imageArray objectAtIndex: 0]; 69 70} 71 72 // case 3 intermediate Page 73 else {74 self. firstimage. image = [self. imageArray objectAtIndex: self. currentPage-1]; 75 self. secondimage. image = [self. imageArray objectAtIndex: self. currentPage]; 76 self. thirimage. image = [self. imageArray objectAtIndex: self. currentPage + 1]; 77} 78 79 self. firstimage. frame = CGRectMake (0, 0, WIDTH, HEIGHT); 80 self. secondimage. frame = CGRectMake (WIDTH, 0, WIDTH, HEIGHT); 81 self. thirimage. frame = CGRectMake (WIDTH * 2, 0, WIDTH, HEIGHT); 82 83 [self. myScrollView addSubview: self. firstimage]; 84 [self. myScrollView addSubview: self. secondimage]; 85 [self. myScrollView addSubview: self. thirimage]; 86 87 self. myScrollView. contentOffset = CGPointMake (WIDTH, 0); 88} 89 90 # pragma mark scrollView Delegate 91 // switch 92-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView 93 {94 float x = self. myScrollView. contentOffset. x; 95 96 // left 97 if (x <0 | x = 0) {98 if (self. currentPage = 0) {99 self. currentPage = (int) self. imageArray. count-1; 100} else {101 self. currentPage --; 102} 103} 104 105 // 106 to the right if (x> 2 * WIDTH | x = 2 * WIDTH) {107 if (self. currentPage = (int) self. imageArray. count-1) {108 self. currentPage = 0; 109} else {110 self. currentPage ++; 111} 112} 113 114 self. myPageControl. currentPage = self. currentPage; 115 [self reloadImage]; 116}View Code