IOS UIPageControl simple instance

Source: Internet
Author: User

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


 

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.