[Objc]-ios Paging Controller implementation

Source: Internet
Author: User

The slider above will slide according to the page

Ideas:

The gray section below is a scrollview where 2 view is placed.

The green slider above is the uilable control.

The green slider below is a 2 button.

Realize:

a layer of Navigationcontroller was set outside all the controllers, so there will be uinavigationbar.

//First ViewController.h   new control and inherit   <uiscrollviewdelegate>

@interface Viewcontroller:baseviewcontroller<uiscrollviewdelegate>//scrollview@property (nonatomic , strong) Uiscrollview * ScrollView; // Moving Slider @property (nonatomic,strong) UILabel *// Select button @property (Nonatomic,strong) UIButton ** view2btn; @end

Lazy loading of the control is implemented in the VIEWCONTROLLER.M file.

#pragmaMark Lazyout-(Uiscrollview *) scrollview{if(!_scrollview) {        //the height of the distance headCGFloat scrollviewheight = -; //the size of the ScrollView position_scrollview = [[Uiscrollview alloc] Initwithframe:cgrectmake (0, Scrollviewheight, Sk_screenwidth,sk_screenheight-scrollviewheight- Self.navigationController.navigationBar.frame.size.height- A)]; _scrollview.showshorizontalscrollindicator=NO; _scrollview.pagingenabled=YES; Cgsize size=_scrollview.frame.size; //width of scrollview = number of pages * Width of individual pagesSize.width *=2;//2 x_scrollview.contentsize = Cgsizemake (Size.width,0); _scrollview.backgroundcolor=[Uicolor Redcolor]; _scrollview.Delegate= self;//Set up proxy            }    return_scrollview;}//Move the slider bar-(UILabel *) navslidelable{if(!_navslidelable) {_navslidelable=[[UILabel alloc] init]; _navslidelable.backgroundcolor= [Uicolor colorwithred: +/255.0Green178/255.0Blue the/255.0Alpha1]; }    return_navslidelable;}//Button-(UIButton *) view1btn{if(!_view1btn) {_view1btn=[[UIButton alloc]init]; //give BTN a target scrollview will move the position according to the tag._view1btn.tag = (Nsinteger) (0/(Self.view.frame.size.width/2)); [_view1btn Setframe:cgrectmake (0,0, sk_screenwidth/2, Top_view_item_height)]; [_view1btn settitle:@"View1"Forstate:uicontrolstatenormal];        [_view1btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; //add the appropriate method to BTN[_view1btn addtarget:self Action: @selector (SWITCHVIEW1BTN:) forcontrolevents:uicontroleventtouchupinside]; }    return_view1btn;}-(UIButton *) view2btn{if(!_view2btn) {_view2btn=[[UIButton alloc]init]; _view2btn.tag= (Nsinteger) (sk_screenwidth/2/(Self.view.frame.size.width/2)); [_view2btn setframe:cgrectmake (Sk_screenwidth/2,0, sk_screenwidth/2, Top_view_item_height)];        [_view2btn settitle:view2 Forstate:uicontrolstatenormal];        [_view2btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];      [_view2btn addtarget:self Action: @selector (SWITCHVIEW2BTN:) forcontrolevents:uicontroleventtouchupinside]; }    return_view2btn;}

Viewdidload adding controls to the page

View in ScrollView I created another 2 UIView class Firstview and Secondview to customize the view

The position of the sliders and buttons can be changed on your own. I wrote it on the top, or I can write it down or somewhere else and change the position.

- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.Self.view.backgroundColor=[Uicolor Whitecolor]; //Add a move slider bar[Self.view addSubview:self.navSlideLable]; //move the initial position of the slider (should be only 2 buttons so the width is directly on the screen more than half of the self adjustment on the good)[Self.navslidelable Setframe:cgrectmake (0,0, sk_screenwidth/2, Top_view_line_height)]; //Add button[Self.view AddSubview:self.view1Btn];    [Self.view AddSubview:self.view2Btn]; //add a view in ScrollView//the position of the first page is 0 0Firstview * Firstview = [[Firstview alloc]initwithframe:cgrectmake (0,0, Sk_screenwidth, Self.scrollView.frame.size.height)]; //the starting position of the second page is the width of the first page, and so on.Secondview * Secondview = [[Secondview alloc]initwithframe:cgrectmake (Self.scrollView.frame.size.width,0, Sk_screenwidth, Self.scrollView.frame.size.height)]; //add a view in ScrollView[Self.scrollview Addsubview:firstview];    [Self.scrollview Addsubview:firstview]; //Add ScrollView[Self.view AddSubview:self.scrollView]; }

Implementing a Button Response method

Here you do not need to change the position of the slider, the button will change the location of the ScrollView, will respond to the ScrollView proxy method to change the position of the slider bar

-(void) SWITCHVIEW1BTN: (UIButton *) button{    // Click button according to the tag of button To change the position of the ScrollView slide    0) animated:yes];} -(void) SWITCHVIEW2BTN: (UIButton *) button{    // Click button according to the tag of button To change the position of the ScrollView slide    0) animated:yes];    }

//Implement ScrollView Proxy method move View location

-(void) Scrollviewdidscroll: (Uiscrollview *) scrollView {    = scrollview.contentoffset;     // there should be only 2 buttons and pages so 2 3 will be sentenced to 3    2 ;     if (x! =0 &&x < scrollView.frame.size.width)       {// Move the position of the slider bar        CGRect frame = self.navSlideLable.frame;         = x;         = frame;    }}

Above????

[Objc]-ios Paging Controller implementation

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.