For iOS, you can customize the startup page and ios

Source: Internet
Author: User

For iOS, you can customize the startup page and ios

In normal development, there may be some special requirements for the startup page, such as adding an animation on the startup page or adding some buttons to respond to events. In recent projects, you need to add a version number on the startup page, because the version number is constantly changing, you need to dynamically add it to the startup page; the Launch Images Source or Launch Screen FIle configured on XCode (the iOS 8 or later will give priority to calling this as the startup Item) both save a static image;

Principle:

In fact, the principle is also very simple. The Launch page still uses the content of Launch Images Source, and then creates a view at the top, and the view background uses the screenshot of the startup Item, it is mistaken that the startup page is still being started. After the startup page is loaded, this layer of view is displayed. After 2 seconds, this layer of view is deleted, resulting in an excessive false startup page effect; the custom actions can be performed on this layer of view. The APP of Coding.net will explain this function below;

I. Create a view EaseStartView

EaseStartView. h file content:

#import <UIKit/UIKit.h>@interface EaseStartView : UIView+ (instancetype)startView;- (void)startAnimationWithCompletionBlock:(void(^)(EaseStartView *easeStartView))completionHandler;@end

EaseStartView. m file content:

#import "EaseStartView.h"#import <NYXImagesKit/NYXImagesKit.h>#import "StartImagesManager.h"@interface EaseStartView ()@property (strong, nonatomic) UIImageView *bgImageView, *logoIconView;@property (strong, nonatomic) UILabel *descriptionStrLabel;@end@implementation EaseStartView+ (instancetype)startView{    UIImage *logoIcon = [UIImage imageNamed:@"logo_coding_top"];    StartImage *st = [[StartImagesManager shareManager] randomImage];    return [[self alloc] initWithBgImage:st.image logoIcon:logoIcon descriptionStr:st.descriptionStr];}- (instancetype)initWithBgImage:(UIImage *)bgImage logoIcon:(UIImage *)logoIcon descriptionStr:(NSString *)descriptionStr{    self = [super initWithFrame:kScreen_Bounds];    if (self) {        //add custom code        UIColor *blackColor = [UIColor blackColor];        self.backgroundColor = blackColor;                _bgImageView = [[UIImageView alloc] initWithFrame:kScreen_Bounds];        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;        _bgImageView.alpha = 0.0;        [self addSubview:_bgImageView];                [self addGradientLayerWithColors:@[(id)[blackColor colorWithAlphaComponent:0.4].CGColor, (id)[blackColor colorWithAlphaComponent:0.0].CGColor] locations:nil startPoint:CGPointMake(0.5, 0.0) endPoint:CGPointMake(0.5, 0.4)];        _logoIconView = [[UIImageView alloc] init];        _logoIconView.contentMode = UIViewContentModeScaleAspectFit;        [self addSubview:_logoIconView];        _descriptionStrLabel = [[UILabel alloc] init];        _descriptionStrLabel.font = [UIFont systemFontOfSize:10];        _descriptionStrLabel.textColor = [UIColor colorWithWhite:1.0 alpha:0.5];        _descriptionStrLabel.textAlignment = NSTextAlignmentCenter;        _descriptionStrLabel.alpha = 0.0;        [self addSubview:_descriptionStrLabel];                [_descriptionStrLabel mas_makeConstraints:^(MASConstraintMaker *make) {            make.centerX.equalTo(@[self, _logoIconView]);            make.height.mas_equalTo(10);            make.bottom.equalTo(self.mas_bottom).offset(-15);            make.left.equalTo(self.mas_left).offset(20);            make.right.equalTo(self.mas_right).offset(-20);        }];        [_logoIconView mas_makeConstraints:^(MASConstraintMaker *make) {            make.centerX.equalTo(self);            make.top.mas_equalTo(kScreen_Height/7);            make.width.mas_equalTo(kScreen_Width *2/3);            make.height.mas_equalTo(kScreen_Width/4 *2/3);        }];                [self configWithBgImage:bgImage logoIcon:logoIcon descriptionStr:descriptionStr];    }    return self;}- (void)configWithBgImage:(UIImage *)bgImage logoIcon:(UIImage *)logoIcon descriptionStr:(NSString *)descriptionStr{    bgImage = [bgImage scaleToSize:[_bgImageView doubleSizeOfFrame] usingMode:NYXResizeModeAspectFill];    self.bgImageView.image = bgImage;    self.logoIconView.image = logoIcon;    self.descriptionStrLabel.text = descriptionStr;    [self updateConstraintsIfNeeded];}- (void)startAnimationWithCompletionBlock:(void(^)(EaseStartView *easeStartView))completionHandler{    [[UIApplication sharedApplication].keyWindow addSubview:self];    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];    _bgImageView.alpha = 0.0;    _descriptionStrLabel.alpha = 0.0;    @weakify(self);    [UIView animateWithDuration:2.0 animations:^{        @strongify(self);        self.bgImageView.alpha = 1.0;        self.descriptionStrLabel.alpha = 1.0;    } completion:^(BOOL finished) {        [UIView animateWithDuration:0.6 delay:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{            @strongify(self);            [self setX:-kScreen_Width];        } completion:^(BOOL finished) {            @strongify(self);            [self removeFromSuperview];            if (completionHandler) {                completionHandler(self);            }        }];    }];}@end

In fact, the most important content in this instance is in the startAnimationWithCompletionBlock method.

    [[UIApplication sharedApplication].keyWindow addSubview:self];    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];

The Code sets this view to the top-most view, so that the page in the program can be covered;

    _bgImageView.alpha = 0.0;    _descriptionStrLabel.alpha = 0.0;

This is to prepare for the following animation. If you use the background image directly, you can set both of them to 0.99 so that there will be no illusion of flash;

    @weakify(self);    [UIView animateWithDuration:2.0 animations:^{        @strongify(self);        self.bgImageView.alpha = 1.0;        self.descriptionStrLabel.alpha = 1.0;    } completion:^(BOOL finished) {        [UIView animateWithDuration:0.6 delay:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{            @strongify(self);            [self setX:-kScreen_Width];        } completion:^(BOOL finished) {            @strongify(self);            [self removeFromSuperview];            if (completionHandler) {                completionHandler(self);            }        }];    }];

Here is the animation effect, and the time is set to 2 seconds, because there is another effect of removing the left start page after the first animation. After the animation ends, you can [self removeFromSuperview];

Ii. Call the startup page view

Call didfinishlaunchingwitexceptions in AppDelegate;

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; // Override point for customization after application launch. self. window. backgroundColor = [UIColor whiteColor]; // network [[sharesharedmanager] setEnabled: YES]; [[AFNetworkReachabilityManager sharedManager] startMonitoring]; // set the navigation bar style [self mimizeinterface]; [[UIApplication sharedApplication] setStatusBarHidden: NO withAnimation: UIStatusBarAnimationFade]; if ([Login isLogin]) {[self setupTabViewController];} else {[UIApplication sharedApplication]. applicationIconBadgeNumber = 0; [self setupIntroductionViewController];} [self. window makeKeyAndVisible]; [FunctionIntroManager showIntroPage]; EaseStartView * startView = [EaseStartView startView]; @ weakify (self); [startView startAnimationWithCompletionBlock: ^ (EaseStartView * easeStartView) {@ strongify (self); // you can do other things}]; return YES ;}

Note that the position of the EaseStartView code should be placed at the end, because it should be loaded later to cover the top layer, so that it can be built on the login page or the home page; this allows you to successfully start the page;

III. The following example dynamically loads the version number used in the project to the startup page.

# Import "StartUpView. h "@ interface StartUpView () @ property (strong, nonatomic) UIImageView * bgImageView; @ property (strong, nonatomic) UILabel * descriptionStrLabel; @ end @ implementation StartUpView + (instancetype) startView {UIImage * bgImage = kshamLaunchImage; return [[self alloc] initWithBgImage: bgImage];}-(instancetype) initWithBgImage :( UIImage *) bgImage {self = [super initWithFrame: Custom]; if (self) {_ bgImageView = [[UIImageView alloc] initWithFrame: Main_Screen_Bounds]; _ bgImageView. contentMode = UIViewContentModeScaleAspectFill; _ bgImageView. alpha = 0; _ bgImageView. image = bgImage; [self addSubview: _ bgImageView]; _ descriptionStrLabel = [[UILabel alloc] init]; _ descriptionStrLabel. font = [UIFont systemFontOfSize: 15]; _ descriptionStrLabel. textColor = [UIColor blackColor]; _ descriptionStrLabel. textAlignment = NSTextAlignmentCenter; _ descriptionStrLabel. alpha = 0; _ descriptionStrLabel. text = [NSString stringWithFormat: @ "version: % @", appVersion]; [self addSubview: _ descriptionStrLabel]; [_ descriptionStrLabel mas_makeConstraints: ^ (MASConstraintMaker * make) {make. height. mas_tables to (50); make. bottom. similar to (self. mas_bottom ). offset (-15); make. left. similar to (self. mas_left ). offset (20); make. right. similar to (self. mas_right ). offset (-20);}];} return self;}-(void) startAnimationWithCompletionBlock :( void (^) (StartUpView * easeStartView) completionHandler {[UIApplication sharedApplication]. keyWindow addSubview: self]; [[UIApplication sharedApplication]. keyWindow bringSubviewToFront: self]; _ bgImageView. alpha = 0.99; _ descriptionStrLabel. alpha = 0.99; @ weakify (self); [UIView animateWithDuration: 2.0 animations: ^ {@ strongify (self); self. bgImageView. alpha = 1.0; self. descriptionStrLabel. alpha = 1.0;} completion: ^ (BOOL finished) {[self removeFromSuperview]; if (completionHandler) {completionHandler (self) ;}}];} @ end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.