For a long time did not write blog, recently in learning iOS development, read a lot of code, their own with Uiscrollview and UIButton to achieve the horizontal scrolling effect, a bit similar to the main page of today's headline interface framework, the effect is as follows:
The code is as follows:
MyScrollView.h
#import <UIKit/UIKit.h>#import"MySegementView.h"@ Interface myscrollview:uiview<uiscrollviewdelegate>-(instancetype) initWithFrame: (CGRect) frame Titlearray: (Nsarray *) Titlearray Viewarray: (Nsarray *) Viewarray; // scrolling page @property (Strong, Nonatomic) Uiscrollview *Myscrollview; // Top label button scrolling view @property (Strong, Nonatomic) Mysegementview *Mysegementview; @end
myscrollview.m
#defineScrollview_width [UIScreen mainscreen].bounds.size.width#defineScrollview_heigth Self.bounds.size.height#defineSEGEMENT_HEIGTHT 44#import "MyScrollView.h"@implementationMyscrollview/*//Only override drawrect:if your perform custom drawing.//an empty implementation adversely affects performance Duri ng animation.-(void) DrawRect: (cgrect) Rect {//Drawing code}*/-(Instancetype) initWithFrame: (CGRect) frame Titlearray: (Nsarray *) Titlearray Viewarray: (Nsarray *) viewarray{ Self=[Super Initwithframe:frame]; if(_mysegementview = =Nil) {_mysegementview= [[Mysegementview alloc] Initwithframe:cgrectmake (0,0, Scrollview_width, segement_heigtht) Titlearray:titlearray block:^void(intIndex) {//implement a callback with block, scroll to the specified position when the top button is clicked[_myscrollview setcontentoffset:cgpointmake (Index-1) * Scrollview_width,0)]; }]; } [self Addsubview:_mysegementview]; [Self addSubview:self.myScrollView]; if(self) { for(inti =0; i < Viewarray.count; i++) {Uiviewcontroller*viewcontroller =Viewarray[i]; ViewController.view.frame= CGRectMake (i * scrollview_width,0, Scrollview_width, self.myScrollView.frame.size.height); [Self.myscrollview AddSubview:viewController.view]; } self.myScrollView.contentSize= Cgsizemake (Viewarray.count * scrollview_width,0); } returnSelf ;}//scrolling Page view lazy loading-(Uiscrollview *) myscrollview{if(_myscrollview = =Nil) {_myscrollview= [[Uiscrollview alloc] Initwithframe:cgrectmake (0, _mysegementview.frame.size.height, Scrollview_width, Scrollview_heigth-_mysegementview.frame.size.height)]; _myscrollview.backgroundcolor=[Uicolor Clearcolor]; _myscrollview.Delegate=Self ; _myscrollview.showsverticalscrollindicator=NO; _myscrollview.showshorizontalscrollindicator=NO; _myscrollview.bounces=NO; _myscrollview.scrollstotop=NO; _myscrollview.pagingenabled=YES; } return_myscrollview;}//scroll end, Update button below line-(void) Scrollviewdidenddecelerating: (Uiscrollview *) scrollview{if(scrollview==_myscrollview) { intP=_myscrollview.contentoffset.x/Scrollview_width; [_mysegementview setpageindex:p+1]; }}@end
MySegementView.h
#import<UIKit/UIKit.h>typedefvoid(^btnclickedblock) (intindex);@interfaceMysegementview:uiview<uiscrollviewdelegate>{ intNpageindex; intTitlecount; UIButton*currentbtn; Nsmutablearray*Btnarray;}- (void) Setpageindex: (int) NIndex;-(Instancetype) initWithFrame: (CGRect) frame Titlearray: (Nsarray *) Titlearray block: (Btnclickedblock) Clickedblock; @property (nonatomic, copy) Btnclickedblock block; @property ( Strong, nonatomic) Uiscrollview*Segementscrollview, @property (Strong, nonatomic) UIView*Selectedline;@end
mysegementview.m
#import "MySegementView.h"#defineSegement_btn_width 48@implementationMysegementview/*//Only override drawrect:if your perform custom drawing.//an empty implementation adversely affects performance Duri ng animation.-(void) DrawRect: (cgrect) Rect {//Drawing code}*/-(Instancetype) initWithFrame: (CGRect) frame Titlearray: (Nsarray *) Titlearray block: (btnclickedblock) clickedblock{ Self=[Super Initwithframe:frame]; [Self addSubview:self.segementScrollView]; if(self) {[self Setbackgroundcolor:[uicolor colorwithred:0x2d/255.0Green0x2a/255.0Blue0x2b/255.0Alpha1]]; Self.block=Clickedblock; Npageindex=1; Titlecount=Titlearray.count; Btnarray=[Nsmutablearray array]; for(inti =0; i < Titlecount; i++) {UIButton*BTN = [[UIButton alloc] Initwithframe:cgrectmake (i * segement_btn_width,0, Segement_btn_width, the)]; [BTN Settitle:titlearray[i] forstate:uicontrolstatenormal]; Btn.titleLabel.font= [Uifont fontwithname:@"Arial"Size -]; [btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal]; Btn.tag= i +1; [Btn addtarget:self Action: @selector (Btnclick:) Forcontrolevents:uicontroleventtouchdown]; [Self.segementscrollview ADDSUBVIEW:BTN]; [Btnarray ADDOBJECT:BTN]; } self.selectedLine.frame= CGRectMake (0, the, Segement_btn_width,2); [Self.segementscrollview AddSubview:self.selectedLine]; Self.segementScrollView.contentSize= Cgsizemake (Titlecount * segement_btn_width,0); } returnSelf ;}//Lazy Loading-(Uiscrollview *) segementscrollview{if(_segementscrollview = =Nil) {CGRect rect=Self.frame; _segementscrollview=[[Uiscrollview alloc] initwithframe:rect]; _segementscrollview.showshorizontalscrollindicator=NO; _segementscrollview.showsverticalscrollindicator=NO; _segementscrollview.bounces=NO; _segementscrollview.pagingenabled=NO; _segementscrollview.Delegate=Self ; _segementscrollview.scrollstotop=NO; } return_segementscrollview;}//Lazy Loading-(UIView *) selectedline{if(_selectedline = =Nil) {_selectedline=[[UIView alloc] init]; _selectedline.backgroundcolor=[Uicolor Redcolor]; } return_selectedline;}//set the current page and update the top tab- (void) Setpageindex: (int) nindex{if(NIndex! =Npageindex) {Npageindex=NIndex; [Self refreshsegement]; }}- (void) refreshsegement{//Find the top button for the currently selected page for(UIButton *btninchBtnarray) { if(Btn.tag = =Npageindex) {currentbtn=btn; } } //if the page corresponding button is selected beyond the visible range, the top scrolling view scrolls intx =currentbtn.frame.origin.x; if(currentbtn.frame.origin.x + segement_btn_width > self.frame.size.width +self.segementscrollview.contentoffset.x) {[Self.segementscrollview Setcontentoffset:cgpointmake ( Self.segementscrollview.contentoffset.x+ Segement_btn_width,0) Animated:yes]; } Else if(Currentbtn.frame.origin.x <self.segementscrollview.contentoffset.x) {[Self.segementscrollview Setcontentoffset:cgpointmake (CURRENTBTN.F Rame.origin.x,0) Animated:yes]; } //Bottom check mark line scrolling effect[UIView animatewithduration:0.2animations:^{_selectedline.frame= CGRectMake (currentbtn.frame.origin.x, Self.frame.size.height-2, Segement_btn_width,2); }completion:^(BOOL finished) {}];}- (void) Btnclick: (uibutton*) btn{currentbtn=btn; if(Npageindex! =btn.tag) {Npageindex=Btn.tag; [Self refreshsegement]; Self.block (Npageindex); }}@end
How to use:
- (void) viewdidload {[Super viewdidload]; Nsmutablearray*array=[nsmutablearray array];//The tabs that are displayed for(inti =0; I < A; i++) {MyViewController1*viewcontroller1 = [[MyViewController1 alloc] Initwithindex:i +1];//Initwithindex: Custom construction method for displaying page numbering[Array addobject:viewcontroller1];//scrolling the View list} Myscrollview= [[Myscrollview alloc] InitWithFrame:self.view.frame titlearray:@[@"1th Page",@"2nd Page",@"3rd page",@"4th page",@"5th Page",@"6th page",@"7th page",@"8th page",@"9th Page",@"10th page",@"11th page",@"12th Page"] Viewarray:array]; [Self.view Addsubview:myscrollview]; //additional setup after loading the view, typically from a nib.}
SOURCE Download Link: http://download.csdn.net/detail/lzm2625347497/9562677
IOS Uiscrollview + UIButton Achieve horizontal scrolling of pages and Top tabs