1. Analysis
Use the Xib layout, then customize a UIView, parse the xib, and then use the controller to pass in the data and load it onto the controller's view to show
2. Program Structure
3, the code specifically implemented
1 "Xib File
2. Create the class Xmgpageview, associate it with the Xib file, select the Xib file, and then set the class defined in the following "custom class" to manage the Xib
1> (, setting xib associated with Class)
2> The XMGPageView.h file, declare the pagination Picture array property, and a quick-create factory method, as follows:
1 #import <UIKit/UIKit.h>23@interface xmgpageview:uiview4 5 + (instancetype) PageView; 6 7 /* * All picture names that need to be shown */ 8 @property (nonatomic, Strong) Nsarray *imagenames; 9 @end
3> xmgpageview.m file, there are a lot of things to do, such as: Managing controls on Xib files,
1. Overriding initialization method (one-time initialization in the inside)
Xib:awakefromnib
Pure code: initWithFrame
2. Rewrite layoutsubviews, layout child controls inside
3. Receive incoming data from outside, override set method
#import "XMGPageView.h"@interfaceXmgpageview () <UIScrollViewDelegate>@property (Weak, nonatomic) Iboutlet Uiscrollview*SC; @property (weak, nonatomic) Iboutlet Uipagecontrol*Pagecontrol;//Note: Nstimer should be weak@property (Weak, nonatomic) Nstimer *timer;@end@implementationXmgpageview+(instancetype) pageview{#warningNote that there are knowledge points here.return[[[NSBundle Mainbundle] loadnibnamed:@"Xmgpageview"Owner:nil Options:nil] lastobject];}/*To Customize the View: 1. Overriding the initialization method (one-time initialization inside) xib:awakefromnib Pure code: initWithFrame 2. Rewrite layoutsubviews, layout child controls inside 3. Receive incoming Data, overriding the Set method*/- (void) awakefromnib{Self.sc.Delegate=Self ; //1. Hide scroll barsSelf.sc.showsHorizontalScrollIndicator =NO; Self.sc.showsVerticalScrollIndicator=NO; //2. Set other properties of UiscrollviewSelf.sc.bounces =NO; Self.sc.pagingEnabled=YES; //3. Listen for Pagecontrol click events[Self.pagecontrol addtarget:self Action: @selector (Pagecontrolclick:) forControlEvents: Uicontroleventvaluechanged]; //4. Set a custom picture by assigning a value to Uipagecontrol's private property by KVC[Self.pagecontrol setvalue:[uiimage imagenamed:@" Current"] Forkeypath:@"_currentpageimage"]; [Self.pagecontrol setvalue:[uiimage imagenamed:@" Other"] Forkeypath:@"_pageimage"]; //5. Let Uiscrollview switch one page at a time of every incident[self starttimer];}#pragmaMark-Internal monitoring-(ibaction) Pagecontrolclick: (Uipagecontrol *) sender{Self.sc.contentOffset= Cgpointmake (Sender.currentpage * self.sc.frame.size.width,0);}#pragmaMark-Timer-related-(void) starttimer{//turn on the timerSelf.timer = [Nstimer scheduledtimerwithtimeinterval:2.0Target:self selector: @selector (nextPage:) UserInfo:@"LNJ"Repeats:yes]; //The main thread handles other events in a little bit of time to handle Nstimer[[Nsrunloop Mainrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];}//switch to next page- (void) NextPage: (Nstimer *) timer{//1. Get the page number of the next pageNsuinteger page = Self.pageControl.currentPage +1; //2. Determine if page numbers are out of bounds if(Page >=_imagenames.count) { //if it crosses the border, go back to page No. 0 .Self.pageControl.currentPage =0; }Else { //If there is no crossing, go to the next pageSelf.pageControl.currentPage =page; } [self PageControlClick:self.pageControl];}- (void) stoptimer{//Turn off the timer[Self.timer invalidate];}#pragmaMark-uiscrollviewdelegate//whenever you scroll, you call- (void) Scrollviewdidscroll: (nonnull Uiscrollview *) scrollview{//1. Calculate page Numbers//Current Page number = width of offset shift/UiscrollviewCGFloat page = scrollview.contentoffset.x/ScrollView.frame.size.width; intCurrnetpage = page +0.5; //2. Change the page numberSelf.pageControl.currentPage =currnetpage;}//Start dragging- (void) scrollviewwillbegindragging: (nonnull Uiscrollview *) scrollview{[self stoptimer];}//End Drag- (void) scrollviewdidenddragging: (nonnull Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate{[self starttimer]; }- (void) Setimagenames: (Nsarray *) imagenames{_imagenames=Imagenames; //0. Each time you reset the picture, you need to love the previous picture for(UIView *subviewinchself.sc.subviews) {[SubView Removefromsuperview]; } //1. Create a picture//1. Initialize child controls, add pictures for(inti =0; i < _imagenames.count; i++) { //1. Create UiimageviewUiimageview *iv =[[Uiimageview alloc] init]; //2. Create a pictureNSString *imagename =_imagenames[i]; UIImage*image =[UIImage Imagenamed:imagename]; Iv.image=image; //4. Adding to the parent control[Self.sc Addsubview:iv]; } //2. Set the number of page numbers for PagecontrolSelf.pageControl.numberOfPages =_imagenames.count; }- (void) layoutsubviews{[Super Layoutsubviews]; CGFloat width=Self.sc.frame.size.width; CGFloat height=Self.sc.frame.size.height; Nsuinteger Imagecount=Self.imageNames.count; //1. Set the frame for each Uiimageview for(inti =0; i < Imagecount; i++) {Uiimageview*iv =Self.sc.subviews[i]; Iv.frame= CGRectMake (i * width,0, width, height); } //2. Setting the scroll rangeSelf.sc.contentSize = Cgsizemake (Imagecount *width, height);}@end
3 "In the controller, display the data to the view can be.
1 #import "ViewController.h"2 #import "XMGPageView.h"3 4 @interfaceViewcontroller () <UIScrollViewDelegate>5 6@property (nonatomic, strong) Xmgpageview *PageView;7 @end8 9 @implementationViewcontrollerTen One- (void) Viewdidload A { - [Super Viewdidload]; - /* the 1. Using Uiscrollview to achieve product display - 2. Package picture carousel with pure code - */ - + //1. Create a picture Carousel -Xmgpageview *pageview =[Xmgpageview PageView]; + //2. Set the frame of the picture Carousel APageview.imagenames = @[@"img_01",@"img_02",@"img_03",@"img_04",@"img_05"]; atPageview.frame = CGRectMake ( -, the, the, -); - //pageview.frame = CGRectMake (0, $, n/a); - [Self.view Addsubview:pageview]; -Self.pageview =PageView; - - } in -- (void) Touchesbegan: (nonnull nsset<uitouch *> *) touches withevent: (Nullable uievent *)Event to { + //Toggle pictures on a picture carousel -Self.pageView.imageNames = @[@"img_04",@"img_05"]; the *}
OK, the picture carousel is over, tomorrow will continue to explain, AutoLayout screen layout, see you tomorrow, ^_^
uiscrollview--Picture Carousel Encapsulation Implementation (III)--(third speaking)