Uiscrollview and Uipagecontrol implement app launch slide diagram

Source: Internet
Author: User
Tags home screen

First, the use of nsuserdefaults to determine whether the slide diagram has occurred, loading slide diagram

Nsuserdefaults Introduction:

       nsuserdefaults can add data permanent save in the phone, he is a singleton, easy to use, So well suited for save simple data and for Data do tag

  you can choose to APPDELEGATE.M didfinishlaunch "); Ingwithoptions method or "initial interface "(Load the first viewcontroller) of   viewdidload method to determine if the slide diagram has occurred, write the code:


Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];//determine if the slide graph has occurred, the first call "Isscrollviewappear" this key The corresponding value is nil, which enters if if (![    @ "YES" isequaltostring:[userdefaults objectforkey:@ "Isscrollviewappear"]) {[Self showscrollview];//show slide chart }


Second, initialize Uiscrollview and Uipagecontrol to prepare for the page slide diagram.

Uiscrollview : With sliding effect, after adding Uiimageview to set up the agent , you can realize the effect of page flipping display picture

Related properties involved:

Contentsize: uiscrollview display size, this is based on demand

Pagingenabled: Whether to support a page-by-page turn, set to Yes, each time sliding, flipping a page;

Bounces: Whether support rebound, this is an animation effect, you can set to see, General slide chart set to No;


Showshorizontalscrollindicator: Whether to show the horizontal slide bar


Uipagecontrol : Commonly used in conjunction with Uiscrollview, pointing to the number of pages that Uiscrollview currently refers to

Related properties involved:

Numberofpages: There are a few points (several pages), this to be set to the same number of pages of the slide graph



Related code:

#pragma  mark -  slide diagram-(void)  showscrollview{    uiscrollview *_ scrollview = [[uiscrollview alloc] initwithframe:[uiscreen mainscreen].bounds];           //set the size of the display content of uiscrollview , there are n graphs to display, set   screen width *n &NBSP, this assumes that you want to display 4 images     _scrollview.contentsize = cgsizemake ([UIScreen  Mainscreen].bounds.size.width * 4, [uiscreen mainscreen].bounds.size.height);         _scrollView.tag = 101;         //Set page flipping effect, do not allow bounce, do not show horizontal slider, set proxy for yourself     _scrollView.pagingEnabled = YES;     _scrollview.bounces = no;    _ scrollview.showshorizontalscrollindicator = no;    _scrollview.delegate =  self;        //Join  UIImageView    for  on uiscrollview  (int i = 0 ;  i < 4; i ++)  {                 UIImageView *imageView = [[UIImageView alloc]  Initwithframe:cgrectmake ([uiscreen mainscreen].bounds.size.width * i , 0, [ Uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height)];                 //the picture that will be loaded into imageview  Zhong         uiimage *image = [uiimage imagenamed:[ nsstring stringwithformat:@ "%d", i+1]];        imageview.image  = image;                 [_scrollview addsubview:imageview];    }        //Initialize  uipagecontrol   and  _scrollView  display in   same page     uipagecontrol *pageconteol =  [[uipagecontrol alloc] initwithframe:cgrectmake (140, self.view.frame.size.height - &NBSP;60,&NBSP;50,&NBSP;40)];    pageconteol.numberofpages = 4;//Settings PageConteol   page  as much as the  _scrollView  pictures     pageConteol.tag = 201;         [self.view addsubview:_scrollview];    [ Self.view addsubview: pageconteol];}



Third, the implementation of Uiscrollview agent method

-(void) scrollviewdidenddecelerating: (uiscrollview *) ScrollView Introduction:


As the name implies, this is a method of sliding the end of deceleration, where the uipagecontrol and uiscrollview


The use of the set, slide diagram disappears information, etc.:


Related properties are involved:


Uiscrollview:


Contentoffset: offset of uiscrollview (sliding position)


Uipagecontrol:

CurrentPage: Current page of Uipagecontrol



Related code:



#pragma  mark - UIScrollViewDelegate-  (void) scrollviewdidenddecelerating: (Uiscrollview  *) scrollview{        //  record the current position of the scrollview  because the paging effect has been set , so: location/Screen size  =  page     int current = scrollview.contentoffset.x/[ uiscreen mainscreen].bounds.size.width;        //according to ScrollView The   position is assigned to the current page of page      UIPageControl *page =  (uipagecontrol *) [ self.view viewwithtag:201];    page.currentpage = current;         //when the last page is displayed, let the slide graph disappear     if  (page.currentpage = =&NBSP;3)  {                 //call method to make the slide graph disappear         [self scrollViewDisappear];     }}


Iv. removing uiscrollview from the screen, and save the information

uiscrollview and remove and do not forget and of course you can set some animation effects , or you need to click to remove it, the code given below is a simple animation effect:

code as follows:

-(void) scrollviewdisappear{    //get  view   UIScrollView  and   uipagecontrol    uiscrollview *scrollview =  (UIScrollView *) [Self.view  viewWithTag:101];    UIPageControl *page =  (uipagecontrol *) [ self.view viewwithtag:201];    //set the slide chart to disappear animation     [UIView  animatewithduration:3.0f animations:^{                 scrollview.center = cgpointmake (self.view.frame.size.width/2, 1.5  * self.view.frame.size.height);             } completion:^ (bool finished)  {                 [scrollView removeFromSuperview];         [page removefromsuperview];    }];        //to save the slide graph's start-up information to  NSUserDefaults  so that the second time do not run the slide diagram     nsuserdefaults *userdefaults = [ nsuserdefaults standarduserdefaults];    [userdefaults setobject:@ "YES"   forkey:@ "Isscrollviewappear"];}


This slide diagram code changes slightly, you can add the timing page, album browsing, click Connect to the corresponding event, etc., the principle is the same.

Related Article

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.