IOS: UISCROLLVIEW and UIPAGECONTROL In the UI Series
In a twinkling of an eye, it's another day. It's just a busy day, but it's okay, it's not just a bad luck, it's just a small success. You can get something done, hey! Well, it's time to sum up. Let's briefly explain what I learned today. I hope it will be helpful to you and a kind of encouragement to you, achieve a win-win situation! First, during the course, we briefly introduced UIScrollView, which is a rolling view inherited from UIView and does not have its own initialization method, therefore, the creation process of the parent class is described below: copy the code UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: self. view. bounds]; scrollView. backgroundColor = [UIColor redColor]; // set the size of the content page. The content must be larger than the size of the frame to scroll. // if the size of the content page is not set, the default size is the same as that of the frame. [scrollView setContentSize: CGSizeMake (320,568*3)]; // you can specify whether the scroll bar is visible. // horizontal scroll bar scrollView. showsHorizontalScrollIndicator = YES; // The vertical scroll bar scrollView. showsVerticalScrollIndicator = YES; // sets the offset of the content page. The default content page starts scrollView from (0, 0. contentOffset = CGPointMake (0, 0); // you can specify whether to slide the entire page. pagingEnabled = NO; // sets whether the boundary is scrollView. bounces = YES; // scroll to the top scrollView. scrollsToTop = YES; // you can specify whether to scroll the scrollView. scrollEnabled = YES; // set whether the boundary is rebounded. The scrollView is valid only when the content width or height is smaller than the frame width or height. alwaysBounceVertical = YES; scrollView. alwaysBounceHorizontal = YES; // zoom. The zoom must be combined with a proxy (delegate) to take effect. Specify the view on the scrollView to zoom in. // set the current zoom ratio. The default value is 1.0. zoomScale = 1.0; // set the maximum scaling ratio to scrollView. maximumZoomScale = 2.0; // you can specify the minimum scale for scrollView. minimumZoomScale = 0.5; // set delegate scrollView. delegate = self; [self. view addSubview: scrollView]; [scrollView release]; imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "image"]; imageView. frame = CGRectMake (0, 0,320,568*3); [scrollView addSubview: imageView]; [imageView release]; common methods for copying code the delegate proxy are as follows: Copy code-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} // call-(void) scrollViewDidScroll when rolling scrollView: (UIScrollView *) scrollView {NSLog (@ "% s", _ FUNCTION __);} // call-(void) scrollViewDidZoom when scaling occurs: (UIScrollView *) scrollView {NSLog (@ "% s", _ FUNCTION __);} // to which view in the ScrollView is scaled-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView {return imageView;} // call-(void) when you are about to start dragging) scrollViewWillBeginDragging :( UIScrollView *) scrollView {NSLog (@ "% s", _ FUNCTION _);} // call-(void) scrollViewWillEndDragging (UIScrollView *) scrollView withVelocity :( CGPoint) velocity targetContentOffset :( inout CGPoint *) targetContentOffset {NSLog (@ "% s", _ FUNCTION __);} // call-(void) scrollViewDidEndDragging (UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {NSLog (@ "% s", _ FUNCTION __);} copying the code feels that UIScrollView paves the way for the next step, so that after the page number switching and scrolling of the image is realized, we talk about UIPageControl: page number controller, like UIControl and UIScrollView, it does not have its own initialization method. You also need to use its own parent class method. The specific creation method is as follows: copy the code-(void) viewDidLoad {[super viewDidLoad]; // UIPageControl: page number controller, inherited from UIControl UIPageControl * pageControl = [[UIPageControl alloc] init]; pageControl. frame = CGRectMake (0,100,320, 30); pageControl. backgroundColor = [UIColor blackColor]; pageControl. numberOfPages = 10; // sets the unselected color pageControl. pageIndicatorTintColor = [UIColor yellowColor]; // sets the selected color pageControl. currentPageIndicatorTintColor = [UIColor blueColor]; // hide pageControl when only one page is set. hidesForSinglePage = NO; // association method [pageControl addTarget: self action: @ selector (changePage :) forControlEvents: UIControlEventValueChanged]; [self. view addSubview: pageControl]; [pageControl release];}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} -(void) changePage :( UIPageControl *) pageControl {// the page number is NSLog (@ "% d", pageControl. currentPage + 1);} copy the code. Here we will use an exercise to further understand these spaces: by calling the method through Page scrolling and page number switching, the effect is similar to that of page turning through the e-books. Below I will attach the code for your reference: copy the code-(void) changePage :( UIPageControl *) pageControl {if (pageControl. currentPage = 0) {[scrollView1 setContentOffset: CGPointMake (0, 0) animated: YES];} else if (pageControl. currentPage = 1) {[scrollView1 setContentOffset: CGPointMake (320, 0) animated: YES];} else if (pageControl. currentPage = 2) {[scrollView1 setContentOffset: CGPointMake (320*2, 0) animated: YES] ;}}- (void) custom :( UIScrollView *) scrollView {pageControl1.currentPage = scrollView. contentOffset. x/320;}-(void) close :( UIButton *) button1 {// if the parent view of the Child view is removed, the Child view will also be removed [button removeFromSuperview]; [scrollView1 removeFromSuperview]; [pageControl1 removeFromSuperview];} copy the Code where-(void) close :( UIButton *) button1 is used to jump to the homepage, just like some new features will be displayed when we open an application.