一.设置引导页
1.创建XLGuideCell类并且同时创建一个Xib文件
关联控件
2.预加载的时候设置立即体验按钮的圆角和边框线
- (void)awakeFromNib {
[super awakeFromNib];
//设置圆角
self.experienceBtn.layer.cornerRadius = _experienceBtn.frame.size.height/2;
//设置边框线的颜色
self.experienceBtn.layer.borderWidth = 2;
self.experienceBtn.layer.borderColor = [UIColor greenColor].CGColor;
}
3.按钮的事件关联,用于移除当前界面并释放
- (IBAction)experienceBtnDidClicked:(UIButton *)sender {
//跳转到解锁界面
UIView *vc = [UIApplication sharedApplication].keyWindow.subviews.lastObject;
//设置已经显示过引导界面
[XLFactory guided];
//渐变跳转
[UIView animateWithDuration:0.5 animations:^{
vc.alpha = 0;
}completion:^(BOOL finished) {
[vc removeFromSuperview];
//尽量不要在window上加视图,会出现内存问题
//释放控制器
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate releaseVC];
}];
}
4.创建一个XLGuideViewController类作为我们的引导界面
#import "XLGuideViewController.h"
#import "XLGuideCell.h"
//定义一个静态的标识符
static NSString *const registerID = @"cellID";
@interface XLGuideViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**接受引导图片*/
@property (nonatomic,strong) NSArray *images;
/**collection的容器视图*/
@property (nonatomic,strong) UICollectionView *collectionView;
/**页数显示*/
@property (nonatomic,strong) UIPageControl *pageControl;
@end
@implementation XLGuideViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self collectionViewInit];
[self pageControllerInit];
}
#pragma mark -------UICollectionView的相关配置 ---------
-(void)collectionViewInit{
//创建一个布局
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//横向滚动
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//item的大小
flowLayout.itemSize = self.view.frame.size;
//item之间的横向距离
flowLayout.minimumLineSpacing = 0;
//创建
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
//按页显示
_collectionView.pagingEnabled = YES;
//不反弹
_collectionView.bounces = NO;
//隐藏横向滚动条
_collectionView.showsHorizontalScrollIndicator = NO;
//代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
//显示
[self.view addSubview:_collectionView];
//注册
[_collectionView registerNib:[UINib nibWithNibName:@"XLGuideCell" bundle:nil]
forCellWithReuseIdentifier:registerID];
}
#pragma mark -------UIPageControl的相关配置 ---------
-(void)pageControllerInit{
//创建pageControl
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.height-100, self.view.width, 50)];
//设置正常状态颜色
_pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
//设置选中的颜色
_pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
//添加方法
[_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
//显示
[self.view addSubview:_pageControl];
}
#pragma mark -------重写images的get方法 懒加载 ---------
-(NSArray *)images{
if (_images == nil) {
//加载数据
self.images = [XLDataOperation guideImages];
//设置总共的页数
_pageControl.numberOfPages = _images.count;
}
return _images;
}
#pragma mark -------UICollectionView配置 ---------
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//返回引导图的个数
return self.images.count;
}
-(XLGuideCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//创建
XLGuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
//给图片
cell.imageView.image = self.images[indexPath.row];
//判断是否是最后一张图片,如果是显示按钮
//最好不要这么做,应该交给cell自己做
if (indexPath.row == self.images.count-1) {
cell.experienceBtn.hidden = NO;
}else{
cell.experienceBtn.hidden = YES;
}
//返回
return cell;
}
#pragma mark -------页面随着页数而动 ---------
-(void)pageChanged:(UIPageControl *)page{
//让collection滚动到相应的页面
//1.创建IndexPath
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:page.currentPage inSection:0];
//2.横向滚动indexPath对应的item
[_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
#pragma mark -------页数随着页面而动 ---------
//设置pageControl随着collection的运动而变化
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//获的页数
int page = _collectionView.contentOffset.x/self.view.width;
//设置为相关页数
_pageControl.currentPage = page;
}
@end
二.设置广告界面
1.创建XLAdViewController类并且同时创建一个Xib文件
关联控件
约束条件注意
2.网络请求示意图
3.通过一个NSURL得到一个request发起链接(老方法)
- (void)viewDidLoad {
[super viewDidLoad];
//1.获取字符串
NSString *str = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1555431276825&di=f6de13e3bae100df9f2be9b14e305df4&imgtype=0&src=http%3A%2F%2Fuploads.5068.com%2Fallimg%2F1801%2F160I4E61-7.jpg";
//2.转化为URL NSString->NSURL
NSURL *url = [NSURL URLWithString:str];
//同步下载
//NSData *imgData = [NSData dataWithContentsOfURL:url];
//3.得到一个request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//4.发起链接 老的方法
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
4.遵从NSURLConnectionDataDelegate协议,实现代理方法
#pragma mark -------得到服务器返回来的响应信息 包含请求数据的详细信息 ---------
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
//NSURLResponse->NSHTTPURLResponse
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse *)response;
//获取响应的头部信息
NSDictionary *headerDic = httpResp.allHeaderFields;
//获取文件的大小
long long size = [[headerDic objectForKey:@"Content-Length"] longLongValue];
//创建保存数据的容器
self.imgData = [NSMutableData dataWithCapacity:size];
}
#pragma mark -------获取一个数据包 可能多次执行 一个文件会分多个包进行转发 ---------
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//追加数据
[_imgData appendData:data];
}
#pragma mark -------文件下载完毕 ---------
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//NSData->UIImage
UIImage *img = [UIImage imageWithData:self.imgData];
//显示图片
self.adView.image = img;
}
5.视图加载完毕后,显示下载好的视图,并给一个跳转按钮
#pragma mark -------视图已经将加载好了 ---------
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//设置按钮标题的内间距
self.jumpBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
//设置初始时间
_totalTime = 2;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
//使用计时器 倒数
NSString *str = [NSString stringWithFormat:@"%d",_totalTime];
//更改显示的时间
self.timeLabel.text = str;
//倒计时 减1
_totalTime--;
//判断是否结束
if (self.totalTime == 0) {
//进入密码页面
[self jump:nil];
}
}];
}
#pragma mark -------跳转按钮 ---------
- (IBAction)jump:(UIButton *)sender {
//关闭定时器
[self.timer invalidate];
//获取windows 的一个子视图 (广告页的视图)
UIView *vc = [UIApplication sharedApplication].keyWindow.subviews.lastObject;
//渐变跳转
[UIView animateWithDuration:0.5 animations:^{
vc.alpha = 0;
}completion:^(BOOL finished) {
//删除广告页面
[vc removeFromSuperview];
//appdeelegate -> vc
//获取引用程序唯一的代理
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate releaseVC];
}];
}
三.AppDeletegate设置界面加载
1.首先将AppDelegate的内存管理设置为手动管理
工程->Build Phases->Compile Sources->AppDelegate.m双击输入-fno-objc-arc
2.在AppDelegate.h中提供一个strong关键字修饰的属性变量接受视图,防止我们还需要当前视图的时候就被释放掉,并且提供一个方法来释放我这个视图
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
/**接受我们创建的视图 不能被删除,用strong强引用*/
@property (nonatomic,strong) UIViewController *vc;
//提供一个方法用于释放引导页和广告页的控制器
-(void)releaseVC;
@end
AppDelegate.m中
//MVC管理内存 释放
-(void)dealloc{
//释放对象
[self.vc release];
[super dealloc];
}
//单纯的释放控制器对象
-(void)releaseVC{
//释放时间不确定,有可能立即,有可能过几秒
[self.vc release];
}
3.创建窗口,设置根视图控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置根控制器
_window.rootViewController = [[XLChooseUnlockWayViewController new] autorelease];
//显示窗口
[_window makeKeyAndVisible];
//是否是新版本
if ([XLFactory isNewVersion]) {
//更新后-引导页
self.vc = [[XLGuideViewController new] autorelease];
//不管有没有按立即体验,在此打开都不会有引导界面
}else{
//正常加载-广告页
self.vc = [[XLAdViewController new] autorelease];
}
//视图的大小
_vc.view.frame = _window.bounds;
//显示vc
[_window addSubview:self.vc.view];
//更新的操作
return YES;
}