- Objective
The Guide page, a cool page, since the micro-Bo used to fire up, for now an app if there is no guide page seems to be so not connected to the atmosphere, then in order to let our app also "tall" once, I wrote a demo to implement the boot page implementation, given my obsessive-compulsive disorder, Use is also as simple as possible, this is not, a code is done, and support version updated to show the new guide page, first look at the effect:
Launchintroduction.gif
The demo encapsulates two methods for invocation, one on the last page of the scrolling view with the Enter button, one without a button, and a direct scroll to hide the boot page.
- Characteristics
1, easy to use, a code to solve
2. Support Custom Indicator Color
3, support the application upgrade after the display of the new guide page
- : Launchintroductiondemo Download
First of all, the use of the method, too late for children's boots can first taste:
1. Import launchintroductionview.m and LaunchIntroductionView.h files into the project
2. Include the header file #import "LaunchIntroductionView.h" in Appdelegate
3. Call:
[Launchintroductionview sharedwithimages:@[@ "launch0.jpg", @ "launch1.jpg", @ "launch2.jpg", @ "Launch3"];
4. There is also a call method, which is a button:
[Launchintroductionview sharedwithimages:@[@ "launch0.jpg", @ "launch1.jpg", @ "launch2.jpg", @ "Launch3"] buttonImage: @ "Login" Buttonframe:cgrectmake (KSCREEN_WIDTH/2-551/4, kscreen_height-150, 551/2, 45)];
- Explain
- header File
/*** Select the page's indicator color, default white */@property (nonatomic, strong) Uicolor *currentcolor;/*** Color of the indicator in other states, default */@property (nonatomic, strong) Uicolor *nomalcolor;/*** Without the button of the Guide page, slide to the last page, and then slide to the right to hide the Guide page ** @param imagenames background image Array ** @return Launchintroductionview Object */+ (instancetype) sharedwithimages: (Nsarray *) imagenames; /*** The Guide page with the button * * @param imagenames background image array * @param buttonimagename button Picture * @param frame button ** @return Launchintroductionview object */+ (instancetype) sharedwithimages: (Nsarray *) imagenames buttonimage: ( NSString *) Buttonimagename buttonframe: (CGRect) frame;
- Core implementations
- Implementation Principle
Create a UIView object view, and then add a scrollview,scrollview on the view to add the desired picture and buttons, and so on, then the program first start or apply the upgrade after the view is added to the current window, so that the view is displayed , when you swipe to the end or click the button to enter the program to remove the view from the window, for a better experience, add a 0.5s animation when the view is removed, resulting in a view vanishing effect.
- Detailed explanation
1, the following is the implementation of the call interface, are used in a single case form
thanks to I_steven's words, has been modified, see the article at the bottom of the update, no longer use the form of a single case, but also welcome to watch my article on the use of a single case and to avoid the abuse of single cases
Default to slide to last when not with a button to hide the Guide page #pragma mark-Create a singleton-->> without button+ (instancetype) Sharedwithimages: (nsarray *) imagenames{static Launchintroductionview *launch = NIL; static dispatch_once_t Oncetoken; dispatch_once (&oncetoken, ^{images = Imagenames; Background array isscrollout = yes; Default to slide to last when not with a button launch = [[Launchintroductionview alloc] Initwithframe:0, 0, Kscreen_width, Kscreen_height)]; Launch.backgroundcolor = [uicolor Whitecolor];}); return launch;}
When the button is clicked by default, the Guide page is hidden, the slide does not hide the Guide page, and the button appears on the last page#pragma mark-Create a singleton-->> with button+ (Instancetype) Sharedwithimages: (Nsarray *) imagenamesButtonimage: (NSString *)Buttonimagenamebuttonframe: (cgrect) frame{static Launchintroductionview *launch = Nil; Static dispatch_once_t oncetoken; dispatch_once (&oncetoken, ^{images = Imagenames; Isscrollout = No;//with button by default is the Click button when the boot page is hidden enterbtnframe = Frame;// Button frame enterbtnimage = buttonimagename;//button picture name launch = [[ Launchintroductionview Alloc] Initwithframe:cgrectmake (0, 0, Kscreen_width, Kscreen_height)] BackgroundColor = [Uicolor whitecolor];})
2, the initialization is based on whether the first start or update version after the first start to determine whether to add the boot page to the window#pragma mark-Initialize-(Instancetype) initWithFrame: (CGRect) frame{Self = [Super Initwithframe:frame];if (Self) {Change the color of the monitor indicator using the KVO [Self Addobserver:Self Forkeypath:@ "CurrentColor" options:Nskeyvalueobservingoptionnew Context:Nil]; [Selfaddobserver: Selfforkeypath:@ "Nomalcolor" Options:nskeyvalueobservingoptionnew Context: NIL]; if ([selfIsfirstlauch]) { UIWindow *window = [[uiapplication sharedapplication] windows].lastobject; [Window Addsubview: Self]; [Selfaddimages];} else{[selfremovefromsuperview]; Self = Nil;}} return self ;}
3, version updates or initial start-up judgment#pragma mark-Determines whether it is the first login or version update-(BOOL) isfirstlauch{Get the current version numberNsdictionary *infodic = [[NSBundle Mainbundle] infodictionary];nsstring *currentappversion = Infodic[@ " Cfbundleshortversionstring "]; //get last launch app saved appversion nsstring *version = [[nsuserdefaults Standarduserdefaults] objectforkey:kappversion]; //version upgrade or first login if (Version = nil | |![ Version isequaltostring:currentappversion]) {[[nsuserdefaults Standarduserdefaults] Setobject:currentappversion Forkey:kappversion]; [[nsuserdefaults standarduserdefaults] synchronize]; return yes;} else{return NO;}}
4, the creation of scrolling view is very simple, not specifically introduced, you can download the code to see the implementation of the specific, the next main point I encountered in the demo a little bit of trouble: the direction of the rolling
The demo now uses this kind of judgment:#pragma mark - 判断滚动方向-(BOOL )isScrolltoLeft:(UIScrollView *) scrollView{ //返回YES为向左反动,NO为右滚动 if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x < 0) { return YES; }else{ return NO; }}
One of the Translationinview's official explanations is this:
Translation in the coordinate system of the specified view
English is not very good, my understanding is probably in a particular view of the coordinates, then what exactly is the use of it? Log to see, the results returned as long as the left to slide the screen, by
[scrollView.panGestureRecognizer translationInView:scrollView.superview].x
The resulting value is negative, the inverse is a positive number, and in the beginning of the finger touch is the coordinates of the (0,0) point, so good, so I can judge the direction of the left and right sliding, and then can be based on the belt without button, In the ScrollView delegate in the last interface to determine whether to hide the boot page:
#pragma mark-scrollview delegate-( void) Scrollviewwillbegindragging: (uiscrollview *) scrollview{ int cuttentindex = (int) (Scrollview.contentoffset.x + KScreen _width/2)/kscreen_width; if (Cuttentindex = images.count-1) { //is guaranteed to be on the last page if ([self Isscrolltoleft:scrollview]) {//is left slide if (!isScrollOut) { Span class= "Hljs-comment" >//directly returns return if set to slide cannot hide the boot page;} [self Hideguidview]; //otherwise hides the boot page in case of left Slide}}}
Of course, not all things seem to be taken for granted, the way I started to judge the direction of the slide is not like this, but:
-(BOOL )isScrolltoLeft:(UIScrollView *) scrollView{ BOOL ret = NO; static CGFloat newX = 0; static CGFloat oldX = 0; newX = scrollView.contentOffset.x; if (newX > oldX) { ret = YES; }else{ ret = NO; } oldX = newX; return ret;//返回YES就是向左滑动,返货NO就是向右滑动}
According to ScrollView's contentoffset to judge, this is done in the case of ScrollView bounce Yes is absolutely no problem, bounce if no in the last page is not able to judge the effect of painting, the reason is very simple, But it took me a while, bounce. In the case of No, the ScrollView Contentoffset will not occur when the last page slides to the left, so no matter how the Slide guide page is not hidden in the absence of a button, And that bounce is set to Yes, right? But if the Guide page with a bounce effect, it is really unacceptable, find a multi-data to find the above method, I hope you avoid this pit, less detours.
- Detailed Usage methods
1, without buttons, do not define the color of the indicator:[LaunchIntroductionView sharedWithImages:@[@"launch0.jpg",@"launch1.jpg",@"launch2.jpg",@"launch3"]];
Yes, just one line of code, we just need to pass in the background image.
2, without the button, the color of the custom indicator: LaunchIntroductionView *launch = [LaunchIntroductionView sharedWithImages:@[@"launch0.jpg",@"launch1.jpg",@"launch2.jpg",@"launch3"]]; launch.currentColor = [UIColor redColor]; launch.nomalColor = [UIColor greenColor];
3, with the button, do not customize the color of the indicator:[LaunchIntroductionView sharedWithImages:@[@"launch0.jpg",@"launch1.jpg",@"launch2.jpg",@"launch3"] buttonImage:@"login" buttonFrame:CGRectMake(kScreen_width/2 - 551/4, kScreen_height - 150, 551/2, 45)];
The argument is a little bit more, but it should also be worth it: the background image array, the app button picture, the button frame, a total of three parameters
4, with the button, the color of the custom indicator sharedWithImages:@[@"launch0.jpg",@"launch1.jpg",@"launch2.jpg",@"launch3"] buttonImage:@"login" buttonFrame:CGRectMake(kScreen_width/2 - 551/4, kScreen_height - 150, 551/2, 45)]; launch.currentColor = [UIColor redColor]; launch.nomalColor = [UIColor greenColor];
- Report:
- Download Link: Launchintroductiondemo:https://github.com/hungryboy/launchintroductiondemo
Update 1
First of all, thanks to I_steven's opinion, just beginning to realize this effect when thinking about using a single case is very convenient, but the memory problem with it is really there, perhaps the memory is really small, but this is really a problem, is the problem to change, so I changed:
#pragma mark - 创建对象-->>不带button+(instancetype)sharedWithImages:(NSArray *)imageNames{ images = imageNames; isScrollOut = YES; launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)]; launch.backgroundColor = [UIColor whiteColor]; return launch;}
#pragma mark - 创建对象-->>带button+(instancetype)sharedWithImages:(NSArray *)imageNames buttonImage:(NSString *)buttonImageName buttonFrame:(CGRect)frame{ images = imageNames; isScrollOut = NO; enterBtnFrame = frame; enterBtnImage = buttonImageName; launch = [[LaunchIntroductionView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height)]; launch.backgroundColor = [UIColor whiteColor]; return launch;}
So also gave birth to my use of this single case and avoid the abuse of a single case article, welcome onlookers.
Update 2
Update Time: 2017.01.12
What's NEW: Add Storyboard support
Methods to invoke when using storyboard
It is important to note that this method will cause crash if the item is not used by storyboard when it is created.
IOS a code Fix start Guide page