UI development ---- UIScrollView and UIPageControl simulate the scroll view and uipagecontrol

Source: Internet
Author: User

UI development ---- UIScrollView and UIPageControl simulate the scroll view and uipagecontrol

// Created By Guo Zi

// ================================================ ================

Master leads the door, practice in person! Self-study is king!

// ================================================ ================

UIScrollView:

UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake (20, 50,200,200)]; scrollView. backgroundColor = [UIColor greenColor]; [self. view addSubview: scrollView]; // sets the content size when contentSize is greater than scrollView. frame. size to slide scrollView. contentSize = CGSizeMake (1000,100 0); UIView * view = [[UIView alloc] initWithFrame: CGRectMake (50, 50, 50)]; view. backgroundColor = [UIColor redColor]; [scrollView addSubview: view]; scrollView. showsHorizontalScrollIndicator = YES; scrollView. showsVerticalScrollIndicator = YES; // scrollView. contentOffset = CGPointMake (50, 50); // scrollView. pagingEnabled = YES; UIView * view2 = [[UIView alloc] initWithFrame: CGRectMake (200, 50, 50, 50)]; view2.backgroundColor = [UIColor blackColor]; [scrollView addSubview: view2]; [view2 release]; scrollView. bounces = YES;) scrollView. scrollsToTop = YES; scrollView. scrollEnabled = YES; scrollView. alwaysBounceHorizontal = NO; scrollView. alwaysBounceVertical = YES; scrollView. minimumZoomScale = 0.5; scrollView. maximumZoomScale = 2; scrollView. zoomScale = 1.5; // sets the proxy scrollView. delegate = self;
To implement the zoom function, follow UIScrollViewDelegate and set itself as a proxy.

For example:

- (UIView *)            viewForZoomingInScrollView:            (UIScrollView *)scrollView{   // NSLog(@"%@",NSStringFromCGSize(scrollView.contentSize));    return [scrollView.subviews objectAtIndex:0];   }


// ================================================ ======================================
UIPageView:

 UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(50, 400, 200, 50)];    pageControl.backgroundColor = [UIColor orangeColor];    [self.view addSubview:pageControl];        pageControl.numberOfPages = 5;    pageControl.currentPage = 2;    pageControl.pageIndicatorTintColor = [UIColor blueColor];    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];    [pageControl addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];    
// ================================================ ======================================

Simulation of a rolling View:

UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; // _ scrollView = scrollView; [self. view addSubview: scrollView]; scrollView. pagingEnabled = YES; // scrollView. delegate = self; scrollView. contentSize = CGSizeMake (320*5,568); // put the image name in the array NSArray * imagesName = [NSArray arrayWithObjects: @ "1.png", @" 2.png", @ "3.png ", @ "4.png", @" 5.png", nil]; for (int I = 0; I <imagesName. count; I ++) {UIImage * img = [UIImage imageNamed: [imagesName objectAtIndex: I]; UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (I * 320, 0,320,568)]; imageView. image = img; [scrollView addSubview: imageView]; [imageView release];} UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame: CGRectMake (50,400,200, 50)]; pageControl. numberOfPages = 5; [pageControl addTarget: self action: @ selector (pageChanged :) forControlEvents: UIControlEventValueChanged]; pageControl. pageIndicatorTintColor = [UIColor redColor]; pageControl. currentPageIndicatorTintColor = [UIColor blackColor]; // self. pageControl = pageControl; [self. view addSubview: pageControl]; [pageControl release]; [scrollView release];
Method implementation:

-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {CGPoint offSet = scrollView. contentOffset; int pageCount = offSet. x/320; _ pageControl. currentPage = pageCount;}-(void) pageChanged :( UIPageControl *) control {long currentPage = control. currentPage; CGPoint offSet = CGPointMake (currentPage * 320, 0); // set the scrollView offSet [self. scrollView setContentOffset: offSet animated: YES];}
// ================================================ ======================================
Ideology or ideology, you know ~~~~~~~~



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.