Be familiar with the dynamic creation of some commonly used controls, that is, create with pure code, without interface Builder to drag and drop the control, but also to implement. The basics of something familiar after still feel very confused, do not know what to do, the leadership suggested let me directly see the company's existing app source code, let me take that boot function to imitate out ~
With the goal, the next step is to analyze how to achieve, without a third-party library, the simplest is probably to use Uiscrollview to achieve, the main points are:
- 4 pages
- There's a big picture on each page.
- Each page has an identification (page number) showing the current page
- There should be a skip button on the last page.
First put on the finished product diagram, and then release the following implementation steps.
First, declare some of the necessary properties, and the attached delegate also adds
@interface Guideviewcontroller () <UIScrollViewDelegate> @property (nonatomic, strong) Uiscrollview *scrollview; @property (nonatomic, strong) Nsarray *images; @property (nonatomic, strong) Uipagecontrol *pagecontrol; @end
constants that define a total number of pages
Define total pages Static const Nsinteger pages = 4;
Defining the dimension constants for a screen
#define ScreenHeight [[UIScreen mainscreen] Bounds].size.height#define screenwidth [[UIScreen mainScreen] bounds]. Size.width
To create a uiscrollview and add it to the view, it is important to note that some of the properties of Uiscrollview are described in the following code.
The size of the initialization scrollview-(void) initscrollview{ //scrollView should be equal to the screen size self.scrollview = [[Uiscrollview alloc] Initwithframe:cgrectmake (0, 0, ScreenWidth, screenheight)]; The width of the scrollView should be 4p large self.scrollView.contentSize = cgsizemake (screenwidth * pages, screenheight); Transverse scrolling self.scrollView.alwaysBounceHorizontal = YES; Turn on paging mode self.scrollView.pagingEnabled = YES; Hide scroll bar self.scrollView.showsHorizontalScrollIndicator = NO; Self.scrollView.delegate = self; [Self.view AddSubview:self.scrollView];}
The next step is to fill in the contents of each page of Uiscrollview, in fact, a bunch of pictures and buttons, as well as uipagecontrol for displaying page numbers , dynamically creating Uiimageview, UIButton, Uipagecontrol I think are OK, relatively easy, the problem in the UIButton tied to the point of attack here, write good Click event method is not responding, do not forget to set the picture "userinteractionenabled" Property.
1- (void) Viewdidload {2 [Super Viewdidload];3 //Do any additional setup after loading the view.4 5 [self initscrollview];6 7 //handles images and ScrollView in low-resolution sizes8CGFloat height = (ScreenHeight = =480) ?568: screenheight;9CGFloat width = (ScreenHeight = =480) ? the: screenwidth;Ten One //Roll out the pictures and add them to the ScrollView A for(inti =0; i < pages; i++) { - -Uiimageview *guideimage = [[Uiimageview alloc] initwithframe:cgrectmake (width * I,0, width, height)]; theNSString *img = [[NSString alloc] Initwithformat:@"guide_%d", i+1]; -Guideimage.image =[UIImage imagenamed:img]; - - //if not specified, the button above it cannot respond to the event +guideimage.userinteractionenabled =YES; - + [Self.scrollview addsubview:guideimage]; A at - //Create an "enter" button on the last page - if(i = = pages-1) { -UIButton *btn =[UIButton Buttonwithtype:uibuttontypecustom]; - -Nsinteger Btnwidth = -; inCGFloat paddingleft = (ScreenWidth- -) /2; -CGFloat paddingtop = (ScreenHeight >480) ? (ScreenHeight- -): screenheight; toBtn.frame = CGRectMake (Paddingleft, Paddingtop, Btnwidth, $); +Btn.backgroundcolor =[Uicolor Orangecolor]; -Btn.layer.cornerRadius =18.0; the [btn addtarget:self Action: @selector (onbuttonclicked:) forcontrolevents:uicontroleventtouchupinside]; *[BTN Settitle:@"Experience Now"Forstate:uicontrolstatenormal]; $Btn.layer.borderColor =[Uicolor Whitecolor]. Cgcolor;Panax NotoginsengBtn.layer.borderWidth =2.0; - the [Guideimage addsubview:btn]; + } A the + if(ScreenHeight = =480&& i = =0) { -NSLog (@"To align the text above the first image with the text on the subsequent image, the y-axis needs to move up 20 points%d", i); $Guideimage.frame = CGRectMake (0, -, width, height); $ } - } - the //on the picture, 4, 4s low-resolution move down scrollview position, as far as possible to make the picture content display complete - if(ScreenHeight = =480) {WuyiSelf.scrollView.frame = CGRectMake (0, - -, width, height); the } - Wu - //draw a paging identity AboutCGFloat pointpaddingtop = screenheight >480? ScreenHeight- -: ScreenHeight- *; $Self.pagecontrol = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, Pointpaddingtop, ScreenWidth, +)]; -Self.pageControl.numberOfPages =pages; - [Self.pagecontrol addtarget:self Action: @selector (pagecontrolclicked:) forControlEvents: Uicontroleventvaluechanged]; - [Self.view AddSubview:self.pageControl]; A}
Finally realize the button and Pagecontrol click Response Method, Uiscrollview delegate method scrollviewdidenddecelerating.
1- (void) onbuttonclicked: (UIButton *) sender{2Uialertview *msg = [[Uialertview alloc] Initwithtitle:nil message:@"Boot page Complete" Delegate: Self Cancelbuttontitle:@"OK"Otherbuttontitles:nil, nil];3 [MSG show];4NSLog (@"Done .");5 }6 7 //respond to paging clicks8- (void) pagecontrolclicked: (Uipagecontrol *) Thepagecontrol9 {Ten[_scrollview Setcontentoffset:cgpointmake (screenwidth*thepagecontrol.currentpage, _scrollview.contentoffset.y) Animated:yes]; One } A - #pragmaMark-uiscrollviewdelegate -- (void) Scrollviewdidenddecelerating: (Uiscrollview *) ScrollView the { -Nsinteger page = scrollview.contentoffset.x/ScreenWidth; -Self.pageControl.currentPage =page; -}
Put the Source: Guide.zip
First full iOS Mini Demo: Boot