1. Put the picture into the Images folder and rename it to Images.bundle. Drag into the project
2 drag ScrollView and press scroll view in the upper-left corner to select Deleagte in the View controller.
This time. Specifies that the proxy for scroll view is controller.
Add a proxy to the specified controller
#import <UIKit/UIKit.h> @interface Cszviewcontroller:uiviewcontroller <uiscrollviewdelegate>// Viewpager@property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview; @end
@implementation cszviewcontroller-(void) viewdidload{[Super Viewdidload]; int width=self.view.frame.size.width; int height=self.view.frame.size.height; Remove multiple pages of content path from bundle compressed file NSString *bundlepath=[[nsbundle Mainbundle] pathforresource:@ "Images" oftype:@ "bundle"]; NSBundle *bundle=[[nsbundle alloc] Initwithpath:bundlepath]; Self.imagesarr=[bundle pathsforresourcesoftype:@ "JPG" indirectory:nil];self.scrollview.contentsize=cgsizemake ( Self.imagesarr.count*width,height); Create paging for (int i=0; i<self.imagesarr.count; i++) {uiimageview *itemimage=[[uiimageview alloc] init]; Itemimage.image=[uiimage Imagewithcontentsoffile:self.imagesarr[i]]; Itemimage.frame=cgrectmake (i*width, 0, width, height); [Self.scrollview Addsubview:itemimage]; }//Remove the slider self.scrollview.showshorizontalscrollindicator=no that appears when sliding; Make Viewpager elastic. Split each page self.scrollview.pagingenabled=yes; Create an identity Uipagecontrol *ctr=[[uipagecontrolALLOC] init]; Ctr.bounds=cgrectmake (0, 0, 150, 50); Ctr.center=cgpointmake (width*0.5, height-50); Ctr.currentpageindicatortintcolor=[uicolor Redcolor]; Ctr.pageindicatortintcolor=[uicolor Graycolor]; ctr.currentpage=1; The number of declaration indicators. If not set will not show Ctr.numberofpages=self.imagesarr.count; [Ctr addtarget:self Action: @selector (pageindict:) forcontrolevents:uicontroleventvaluechanged]; [Self.view addsubview:ctr]; Because Self.view is already bound once, Pagecontrol is a weak reference self.pagecontrol=ctr;} #pragma mark Click Indicator Jump viewpager-(ibaction) Pageindict: (ID) sender{int curr=self.pagecontrol.currentpage; NSLog (@ "%d", Curr); Self.scrollview.contentoffset=cgpointmake (Self.view.frame.size.width*curr, 0);} #pragma mark stops when sliding Viewpager to allow the corresponding indicator to change-(void) scrollviewdidenddecelerating: (Uiscrollview *) scrollview{int OffSetX =self.scrollview.contentoffset.x; int index=offsetx/self.view.frame.size.width; Self.pagecontrol.currentpage=index; } @end