博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS关于启动页自定义特殊处理
阅读量:5008 次
发布时间:2019-06-12

本文共 9016 字,大约阅读时间需要 30 分钟。

平常开发中对于启动页可能会有一些特别的要求,比如在启动页加动画或加一些按键可以响应事件等,最近项目中要在启动页增加版本号,因为版本号是不断的改变,所以要动态实现把它加到启动页上;在XCode上面配置的Launch Images Source或Launch Screen FIle(IOS8以上会优先调用这个作为启动项)都是保存一张静态图片;

原理:

其实原理也是很简单,启动页还是运用Launch Images Source的内容,然后在做一个视图在最上层,视图的背景用启动项的那张图,让人误以为还在启动中,启动页加载完成后,就显示这层视图,在2秒后再把这层视图删除,产生一个过度的假启动页效果;而我们自定义的动作就可以在这层视图上进行;下面将通过Coding.net的APP讲解这个功能;

一:创建一个视图EaseStartView

EaseStartView.h文件内容:

#import 
@interface EaseStartView : UIView+ (instancetype)startView;- (void)startAnimationWithCompletionBlock:(void(^)(EaseStartView *easeStartView))completionHandler;@end

EaseStartView.m文件内容:

#import "EaseStartView.h"#import 
#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

其实本实例中最为关键的内容在方法startAnimationWithCompletionBlock里

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

代码就是把这个视图设置成在最前的最上层,这样就可以盖住程序中的页面;

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

这个是为了下面的动画做准备,若是直接用背景图可以把这两个都设置成0.99这样就不会有一闪的错觉;

@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);            }        }];    }];

这边是动画效果,时间设置为2秒,因为这边第一个动画完还有一个左移出启动页的效果;当动画结束后就可以  [self removeFromSuperview];

二:调用启动页视图

在AppDelegate中的didFinishLaunchingWithOptions进行调用;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];        //网络    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];    [[AFNetworkReachabilityManager sharedManager] startMonitoring];        //设置导航条样式    [self customizeInterface];    [[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);        //可以做其它事情    }];        return YES;}

注意,EaseStartView代码的位置是要放在最后面,因为要让它盖在最上层,就要后面加载,这样就可以盖在登录页面上面或者主页上;到这就已经可以成功启动页的效果;

三:下面实例为项目中用到的动态加载版本号到启动页上

#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:Main_Screen_Bounds];    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:@"版本号为:%@",appVersion];        [self addSubview:_descriptionStrLabel];                [_descriptionStrLabel mas_makeConstraints:^(MASConstraintMaker *make) {            make.height.mas_equalTo(50);            make.bottom.equalTo(self.mas_bottom).offset(-15);            make.left.equalTo(self.mas_left).offset(20);            make.right.equalTo(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

 

 

最近有个妹子弄的一个关于扩大眼界跟内含的订阅号,每天都会更新一些深度内容,在这里如果你感兴趣也可以关注一下(嘿对美女跟知识感兴趣),当然可以关注后输入:github 会有我的微信号,如果有问题你也可以在那找到我;当然不感兴趣无视此信息;

转载于:https://www.cnblogs.com/wujy/p/5194835.html

你可能感兴趣的文章
[BZOJ 2818]Gcd
查看>>
FORM值传递与地址传递
查看>>
(译)yaml快速教程
查看>>
C:大数相加
查看>>
160. Intersection of Two Linked Lists
查看>>
人生苦短,我用python-- Day11
查看>>
JAVA Bean
查看>>
ehcache memcache redis 三大缓存男高音_转
查看>>
curd_3
查看>>
百度地图API示例之设置地图显示范围
查看>>
Java构造方法、重载及垃圾回收
查看>>
.Net Core AES加密解密
查看>>
Spring Quartz实现任务调度
查看>>
python | 桶排序、冒泡排序、选择排序、去重
查看>>
两个Html页面之间值得传递
查看>>
EasyUI datagrid 的多条件查询
查看>>
Mac升级bash到最新版本
查看>>
利用vagrant打包系统--制作自己的box
查看>>
美女与硬币问题
查看>>
计算几何算法概览 (转)
查看>>