ios ~ 登录、自定义启动页、首页
2021-12-27 本文已影响0人
阳光下的叶子呵
一、原理:设置rootViewController
在APPdelegate.m 中判断是否登录,跳转登录页、首页。
进入APP,启动页,再判断是否登录,再跳到登录页或首页。
APPdelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
APPdelegate.m
@interface AppDelegate ()<UIApplicationDelegate, UITabBarControllerDelegate,WXApiDelegate>
@property (nonatomic,strong)GWMainTabBarController *tabbarController;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"这是一条测试push");
[NSThread sleepForTimeInterval:0.01]; // 缩短系统启动页时间
// 启动页(自定义):
StartPageAnimationCRTL *startPageAnimationVC = [[GWStartPageAnimationCRTL alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:startPageAnimationVC];
// 自定义启动页加载完毕(控制时间),判断是否登录
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f *NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//判断是否第一次登录
if ([UserInfoContext sharedUserInfoContext].userInfo.token &&[UserInfoContext sharedUserInfoContext].userInfo.name.length !=0) {
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.tabbarController];
} else {
LoginViewController *loginVC = [[GWLoginViewController alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:loginVC];
}
});
}
//添加监听用于登录
-(void)addNotifiMethod{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginMethod:) name:@"login_login" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutMethod:) name:@"login_logout" object:nil];
// 去登录页VC调用
[[NSNotificationCenter defaultCenter] postNotificationName:@"login_login" object:nil];
}
-(void)loginMethod:(NSNotification *)notifi{
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.tabbarController];
}
-(void)logoutMethod:(NSNotification *)notifi{
//退出登录 停止轮训
[[GWRotationService sharedMessageService] stopRefreshTimer];
[GWUtilities removeUserInfoModelInUserDefAultes];
GWLoginViewController *loginVC = [[GWLoginViewController alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:loginVC];
}
二、自定义启动页
原理:多张图片设置动图效果,(最后一张图片可以复制多张一样的,形成最后一张图“停一下”的效果)。
#import "GWStartPageAnimationView.h"
@interface GWStartPageAnimationView ()
@property (nonatomic, strong) UIImageView *animationImg;
@end
@implementation GWStartPageAnimationView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = RGBA(0, 0, 0, 1);
UIImage *image = [UIImage imageNamed:@"startPageAnimation_welcome_icon1.png"];
CGFloat iconHeight = self.frame.size.width * (image.size.height / image.size.width);
_animationImg = [[UIImageView alloc] init];
[self addSubview:self.animationImg];
[self.animationImg makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(self);
make.center.width.equalTo(self);
make.height.mas_equalTo(iconHeight);
}];
self.animationImg.animationImages = [self animationImages]; // 获取 Gif 图片列表
self.animationImg.animationDuration = 1.5; // 执行一次完整动画所需的时长
self.animationImg.animationRepeatCount = 1; // 动画重复次数
[self.animationImg startAnimating];
}
return self;
}
- (NSArray *)animationImages {
NSMutableArray *images = [NSMutableArray arrayWithCapacity:26];
for (int i = 0; i < 33; i++) {
NSString *imageStr = [NSString stringWithFormat:@"startPageAnimation_welcome_icon%d.png",i + 1];
UIImage *image = [UIImage imageNamed:imageStr];
[images addObject:image];
}
return images;
}