[IOS] effect of sliding switching with Uiscrollview and Uipagecontrol

Source: Internet
Author: User

Uipagecontrol is a self-brought control that allows you to view official documents and download the official sample study.

If you are unfamiliar with Xcode documentation, see: Apple Xcode Help documentation read the guide

Next I take notes and use storyboard to achieve the effect of sliding transitions.


-----------------------------------------------------------------------------


Create a new project, drag the previous Uiscrollview and Uipagecontrol, and establish the association:



Create a new Contentviewcontroller, as a content view for page switching:




In other words, when the sliding screen switch, it is actually more than Contentviewcontroller in the switch.


The basic structure is:


On the main page, we first initialize some of the settings:

-(void) viewdidload{[Super viewdidload];//do no additional setup after loading the view, typically from a nib.            Initialize the contents of page control _contentlist = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil];        A total of how many pages nsuinteger numberpages = Self.contentList.count;    Store all controller Nsmutablearray *controllers = [[Nsmutablearray alloc] init];    for (Nsuinteger i = 0; i < numberpages; i++) {[controllers addobject:[nsnull null]];        } self.viewcontrollers = controllers;  The width of a page is ScrollView width self.myScrollView.pagingEnabled = YES;  Auto-Scroll to subview boundary self.myScrollView.contentSize = Cgsizemake (Cgrectgetwidth (self.myScrollView.frame) * numberpages,    Cgrectgetheight (Self.myScrollView.frame));    Self.myScrollView.showsHorizontalScrollIndicator = NO;    Self.myScrollView.showsVerticalScrollIndicator = NO;    Self.myScrollView.scrollsToTop = NO;        Self.myScrollView.delegate = self;    _numberofpages = numberpages; _mypagecOntrol.numberofpages = numberpages;            _currentpage = 0;    [Self loadscrollviewwithpage:0];    [Self loadscrollviewwithpage:1]; }






Next, we need a function to load the elements on the Contentview page:

Load different subviewcontroller-(void) loadscrollviewwithpage in ScrollView: (nsuinteger) page{    if (page >= Self.contentList.count)        return;        Replace the placeholder if necessary    lcontentviewcontroller *controller = [Self.viewcontrollers objectatindex: Page];    if ((NSNull *) controller = = [NSNull null])    {        controller = [[Lcontentviewcontroller alloc] init];        [Self.viewcontrollers replaceobjectatindex:page Withobject:controller];    }        Add the controller ' s view to the scroll view    if (Controller.view.superview = = nil)    {        CGRect frame = self . myscrollview.frame;        frame.origin.x = Cgrectgetwidth (frame) * page;        FRAME.ORIGIN.Y = 0;        Controller.view.frame = frame;        [Controller Setlabel:[_contentlist Objectatindex:page]];        [Self.myscrollview AddSubview:controller.view];}    }

Then we'll deal with the Pagecontrol switching event:

-(void) GotoPage: (BOOL) animated{    nsinteger page = self.myPageControl.currentPage;        Load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)    [Self loa DSCROLLVIEWWITHPAGE:PAGE-1];    [Self loadscrollviewwithpage:page];    [Self loadscrollviewwithpage:page + 1];    Update the scroll view to the appropriate page    cgrect bounds = self.myScrollView.bounds;    bounds.origin.x = Cgrectgetwidth (bounds) * page;    BOUNDS.ORIGIN.Y = 0;    [Self.myscrollview scrollrecttovisible:bounds animated:animated];} The page Control option modifies the listener-(ibaction) Changepage: (ID) sender{    [self gotopage:yes];    YES = animate}

Event monitoring for sliding ScrollView:

Swipe End Event listener-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{    /Switch the indicator when more than 50% of the Previous/next page is visible    cgfloat pagewidth = Cgrectgetwidth (self.myScrollView.frame);    Nsuinteger page = Floor ((SELF.MYSCROLLVIEW.CONTENTOFFSET.X-PAGEWIDTH/2)/pagewidth) + 1;    self.myPageControl.currentPage = page;    NSLog (@ "Current page =%d", page);    A possible optimization would is to unload the views+controllers which is no longer visible        [self LOADSCROLLVIEWWI THPAGE:PAGE-1];    [Self loadscrollviewwithpage:page];    [Self loadscrollviewwithpage:page + 1];}
























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.