IOS UIPageControl simple instance
IOS UIPageControl
Code for automatic scrolling to be supplemented
Add images on your own
. H file code
#import
@interface ViewController : UIViewController
{ UIScrollView *_scrollView; NSMutableArray *slideImages; UIPageControl *_page;}@end
. M file code
# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; _ scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 20,320,240)]; _ scrollView. bounces = NO; _ scrollView. pagingEnabled = YES; _ scrollView. delegate = self; _ scrollView. contentOffset = CGPointMake (320, 0); _ scrollView. contentSize = CGSizeMake (1920,240); _ scrollView. showsVerticalScrollIndicator = NO; _ scrollView. showsHorizontalScrollIndicator = NO; _ scrollView. userInteractionEnabled = YES; [self. view addSubview: _ scrollView]; slideImages = [[NSMutableArray alloc] initWithObjects: @ "1.png", @" 2.png", @ "3.png", @" 4.png", nil]; UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [slideImages objectAtIndex :( [slideImages count]-1)]; imageView. frame = CGRectMake (0, 0,320,240); [_ scrollView addSubview: imageView]; for (int I = 0; I <[slideImages count]; I ++) {// loop this bit UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [slideImages objectAtIndex: I]; imageView. frame = CGRectMake (320 * I + 320, 0,320,240); imageView. userInteractionEnabled = YES; [_ scrollView addSubview: imageView];} imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [slideImages objectAtIndex: 0]; imageView. frame = CGRectMake (320*5, 0,320,240); [_ scrollView addSubview: imageView]; _ page = [[UIPageControl alloc] initWithFrame: CGRectMake (240,230, 70, 30)]; _ page. numberOfPages = 4; _ page. currentPage = 0; // _ page. backgroundColor = [UIColor grayColor]; [_ page addTarget: self action: @ selector (pageAction) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: _ page]; // Do any additional setup after loading the view .} -(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {int currentPage = (_ scrollView. contentOffset. x-_ scrollView. frame. size. width/([slideImages count] + 2)/_ scrollView. frame. size. width + 1; NSLog (@ "% d", currentPage); if (currentPage = 0) {[_ scrollView scrollRectToVisible: CGRectMake (320*4, 0,320,240) animated: NO];} else if (currentPage = ([slideImages count] + 1) {// if it is the last + 1, that is, the first [_ scrollView scrollRectToVisible: CGRectMake (320, 0,320,240) animated: NO] ;}}-(void) scrollViewDidScroll :( UIScrollView *) to start the loop *) sender {int page = _ scrollView. contentOffset. x/320-1; _ page. currentPage = page;}-(void) pageAction {int page = _ page. currentPage; [_ scrollView setContentOffset: CGPointMake (320 * (page + 1), 0)];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end