Last week nothing wrote a simple picture view, the last view only used a uiimageview, constantly replace the background image, to achieve the switch between the pictures. Through the ScrollView can be very simple to implement the view between the picture, set setpagingenabled through the coordinates, the width of the settings, you can simply implement a picture of a simple paging view display, of course, if you have the need to say that needs to carry out the so-called Infinite Loop, At the beginning and the end of the set up the event, the common news client, the picture of the news when the time is very rare to go to the end of the time to jump to the first page, is generally the last time are recommended related content, more said two sentences, start it:
ScrollView Picture Page
The control, like the previous article, is ScrollView:
Drag three photos into the project, the previous article has a demo, the next time blog to consider changing the diagram, initialize ScrollView:
Nsarray *[email protected][@ "girl0.jpg", @ "girl1.jpg", @ "girl2.jpg"]; CGFloat width=self.scrollview.bounds.size.width; CGFloat height=self.scrollview.bounds.size.height; for (Nsinteger i=0; I<[imagearr count]; i++) { UIImage *image=[uiimage imagenamed:imagearr[i]]; Uiimageview *imageview=[[uiimageview alloc] initwithimage:image]; [ImageView setframe:cgrectmake (i*width, 0, width, height)]; [_scrollview Addsubview:imageview]; }
There is no comment here, but a little more bounds is the boundary, which can be understood as the width and height of the ScrollView in the control, that is, the width and height of the previous picture, setting the surrounding boundary:
[Self.scrollview Setbounces:no];
To set the horizontal scroll bar:
[Self.scrollview Setshowshorizontalscrollindicator:no];
Set the overall width of the ScrollView (key):
[Self.scrollview setcontentsize:cgsizemake ([Imagearr count]*width, height)];
Set Paging:
[Self.scrollview Setpagingenabled:yes];
Demo Effect:
Uipagecontrol and ScrollView picture pagination
Uipagecontrol if a little bit of the front-end can be understood as the focus of the map, probably through the instructions, iOS is a small dot, Android need to cut their own graphics or self-reliance, iOS is relatively human, first define a Uipagecontrol:
@property (Nonatomic,strong) Uipagecontrol *pagecontrol;
Initialize the Uipagecontrol setting size and location:
Self.pagecontrol=[[uipagecontrol alloc] init]; Self.pagecontrol.backgroundcolor=[uicolor Clearcolor]; [Self.pagecontrol setbounds:cgrectmake (0, 0,200, +)]; [Self.pagecontrol Setcenter:cgpointmake (width/2,height/2+200.0)];
To set the current page and page size:
Self.pagecontrol.numberofpages=[imagearr Count]; self.pagecontrol.currentpage=0;
Set current and other indications:
[Self.pagecontrol Setcurrentpageindicatortintcolor:[uicolor Greencolor]; [Self.pagecontrol Setpageindicatortintcolor:[uicolor Yellowcolor];
When setting the slide, change the Uipagecontrol and change the ScrollView by Uipagecontrol:
[_scrollview setdelegate:self]; [Self.pagecontrol addtarget:self Action: @selector (switchpage:) forcontrolevents:uicontroleventvaluechanged]; [Self.view Addsubview:_pagecontrol];
When sliding, change the Uipagecontrol instructions through the delegate settings, specific reference to the previous article:
-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{ Nsinteger currentpage= Scrollview.contentoffset.x/self.view.bounds.size.width; [Self.pagecontrol setcurrentpage:currentpage];}
Uipagecontrol Change ScrollView:
-(void) Switchpage: (ID) sender{ uipagecontrol *currentcontrol= (Uipagecontrol *) sender; Nsinteger Currentpage=currentcontrol.currentpage; [_scrollview setcontentoffset:cgpointmake (currentpage*self.view.bounds.size.width, 0)];}
Look at the specific effect:
iOS Development-image view (Scrollview+uipagecontrol)