AJ学IOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现
AJ分享,必须精品
一:效果
这里实现了大多数app都会有的软件新特性的功能,用的是UICollectionViewController实现的
二:思路
这里用了UICollectionViewController实现,就是做一个没有间隙,每个cell都是一个屏幕的UICollectionViewController,自定义的。然后把下面的UIPageControl 还有最后一页的开始以及分享按钮放入就OK了。
调用的时候,首先获取当前的app的版本号,然后再获取上一个版本。进行两个版本的比较。当前版本和上一个版本不同,当前版本是从infoDictionary中拿到的,上一个版本是从自己存的NSUserDefaults 中的NYVersionKey拿到的,并且苹果允许上传小于当前版本的app,如果是第一个版本时候,上一个版本还没有值,也会不同。
根据结果设置window的不同根控制器。
自定义UICollectionViewController步骤:
要注意:
1.初始化的时候设置布局参数
2.collertionView必须注册cell
3.自定义cell
三:代码
调用部分代码:AppDelegate
//1,获取当前的版本号
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
//2,获取上一次的版本号
NSString *lastVersion = [[NSUserDefaults standardUserDefaults]objectForKey:NYVersionKey];
NYLog(@"currentVersion == %@ , lastVersion == %@ ",currentVersion, lastVersion);
//判断是否有新的版本
if ([currentVersion isEqualToString:lastVersion]) {
//如果没有新的版本(当前版本和上一个版本不同,当前版本是从infoDictionary中拿到的,上一个版本是从自己存的NSUserDefaults 中的NYVersionKey拿到的,并且苹果允许上传小于当前版本的app,如果是第一个版本时候,上一个版本还没有值,也会不同。)
//创建tabBarVC
NYTabBarController *tabBarVC = [[NYTabBarController alloc]init];
//设置窗口跟控制器
self.window.rootViewController = tabBarVC;
}else{//如果有新的版本
//进入新特性界面
NYNewFeatureController *newFeatureVC = [[NYNewFeatureController alloc]init];
newFeatureVC.view.backgroundColor = [UIColor redColor];
self.window.rootViewController = newFeatureVC;
//用偏好设置,保存当前版本。
[[NSUserDefaults standardUserDefaults]setObject:currentVersion forKey:NYVersionKey];
}
自定义的collectionViewController
NYNewFeatureController.m
//
// NYNewFeatureController.m
// Created by apple on 15-8-1.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "NYNewFeatureController.h"
#import "NYNewFeatureCell.h"
@interface NYNewFeatureController ()
@property (nonatomic, weak) UIPageControl *control;
@end
@implementation NYNewFeatureController
static NSString * const reuseIdentifier = @"cell";
- (void)viewDidLoad {
[super viewDidLoad];
//注册cell,默认就会创建这个类型的cell
[self.collectionView registerClass:[NYNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
//分页
self.collectionView.pagingEnabled = YES;
//取消弹簧效果
self.collectionView.bounces = NO;
//不显示滚动条
self.collectionView.showsHorizontalScrollIndicator = NO;
// 添加pageController
[self setUpPageControl];
// Do any additional setup after loading the view.
}
// 添加pageController
- (void)setUpPageControl
{
// 添加pageController,只需要设置位置,不需要管理尺寸
UIPageControl *control = [[UIPageControl alloc] init];
control.numberOfPages = 4;
control.pageIndicatorTintColor = [UIColor blackColor];
control.currentPageIndicatorTintColor = [UIColor redColor];
// 设置center
control.center = CGPointMake(self.view.width * 0.5, self.view.height);
_control = control;
[self.view addSubview:control];
}
/*使用UICollectionViewController要注意:
1.初始化的时候设置布局参数
2.collertionView必须注册cell
3.自定义cell
*/
-(instancetype)init
{
//
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//设置cell的尺寸
layout.itemSize = [UIScreen mainScreen].bounds.size;
//清空cell间隔的行距
layout.minimumLineSpacing = 0;
//设置cell的滑动方向
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
return [super initWithCollectionViewLayout:layout];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UICollectionView代理和数据源
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// dequeueReusableCellWithReuseIdentifier
// 1.首先从缓存池里取cell
// 2.看下当前是否有注册Cell,如果注册了cell,就会帮你创建cell
// 3.没有注册,报错
NYNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// 拼接图片名称 3.5 320 480
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld",indexPath.row + 1];
if (screenH > 480) { // 5 , 6 , 6 plus
imageName = [NSString stringWithFormat:@"new_feature_%ld-568h",indexPath.row + 1];
}
cell.image = [UIImage imageNamed:imageName];
[cell setIndexPath:indexPath count:4];
return cell;
}
#pragma mark - UIScrollView代理
// 只要一滚动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 获取当前的偏移量,计算当前第几页
int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
// 设置页数
_control.currentPage = page;
}
@end
自定义的cell
NYNewFeatureCell.h
//
// NYNewFeatureCell.h
// Created by apple on 15-8-1.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface NYNewFeatureCell : UICollectionViewCell
@property (nonatomic, strong) UIImage *image;
// 判断是否是最后一页
- (void)setIndexPath:(NSIndexPath *)indexPath count:(int)count;
@end
NYNewFeatureCell.m
//
// NYNewFeatureCell.m
// Created by apple on 15-8-1.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "NYNewFeatureCell.h"
#import "NYTabBarController.h"
@interface NYNewFeatureCell()
@property (nonatomic, weak) UIImageView *imageView;
//分享按钮
@property (nonatomic, weak) UIButton *shareButton;
//开始按钮
@property (nonatomic, weak) UIButton *startButton;
@end
@implementation NYNewFeatureCell
//分享按钮懒加载
- (UIButton *)shareButton
{
if (_shareButton == nil) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"分享给大家" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn sizeToFit];
[self.contentView addSubview:btn];
_shareButton = btn;
}
return _shareButton;
}
//开始按钮懒加载
- (UIButton *)startButton
{
if (_startButton == nil) {
UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
[startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
[startBtn sizeToFit];
[startBtn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:startBtn];
_startButton = startBtn;
}
return _startButton;
}
-(UIImageView *)imageView
{
if (_imageView == nil) {
UIImageView *imageV = [[UIImageView alloc]init];
_imageView = imageV;
// 注意:一定要加载contentView
[self.contentView addSubview:imageV];
}
return _imageView;
}
// 布局子控件的frame
-(void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = self.bounds;
// 分享按钮
self.shareButton.center = CGPointMake(self.width * 0.5, self.height * 0.8);
// 开始按钮
self.startButton.center = CGPointMake(self.width * 0.5, self.height * 0.9);
}
-(void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
// 判断当前cell是否是最后一页
-(void)setIndexPath:(NSIndexPath *)indexPath count:(int)count
{
if (indexPath.row == count - 1) { // 最后一页,显示分享和开始按钮
self.shareButton.hidden = NO;
self.startButton.hidden = NO;
}else{ // 非最后一页,隐藏分享和开始按钮
self.shareButton.hidden = YES;
self.startButton.hidden = YES;
}
}
// 点击开始微博的时候调用
- (void)start
{
// 进入tabBarVc
NYTabBarController *tabBarVc = [[NYTabBarController alloc] init];
// 切换根控制器:可以直接把之前的根控制器清空
NYKeyWindow.rootViewController = tabBarVc;
}
@end
AJ学IOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现的更多相关文章
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...
- AJ学IOS 之微博项目实战(8)用AFNetworking和SDWebImage简单加载微博数据
AJ分享,必须精品 一:效果 没有图文混排,也没有复杂的UI,仅仅是简单的显示出微博数据,主要介绍AFNetworking和SDWebImage的简单用法 二:加载数据AFNetworking AFN ...
- AJ学IOS 之微博项目实战(1)微博主框架-子控制器的添加
AJ分享,必须精品 一:简单介绍 这是新浪微博的iOS端项目,来自于黑马的一个实战项目. 主要分成五大模块,本次全部运用纯代码实现,其中会用到很多前面学过得内容,如果有的地方有重复的知识点,说明这个知 ...
- AJ学IOS 之微博项目实战(5)微博自定义搜索框searchBar
AJ分享,必须精品 一:效果 用UITextField简单定义一个搜索框 二:调用: 调用的代码,很简单,直接init就可以,以后加功能自己添加就行了. - (void)viewDidLoad { [ ...
- AJ学IOS 之微博项目实战(10)微博cell中图片的显示以及各种填充模式简介
AJ分享,必须精品 :一效果 如果直接设置会有拉伸等等的状况,这里主要介绍图片显示的一些细节 二:代码 代码实现其实很简单,微博当中用了一个photos来存放九宫格这些图片,然后用了一个photo类来 ...
- AJ学IOS 之微博项目实战(9)微博模型之时间相关重要操作,判断刚刚,昨天,今年等等
AJ分享,必须精品 一:效果 二:实现代码 /** 1.今年 1> 今天 * 1分内: 刚刚 * 1分~59分内:xx分钟前 * 大于60分钟:xx小时前 2> 昨天 * 昨天 xx:xx ...
- AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮
AJ分享,必须精品 一:效果图 自定义tabBar实现最下面中间的添加按钮 二:思路 首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这 ...
- AJ学IOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机
AJ分享,必须精品 一:效果 二:代码 相机部分就简单多了,几行代码调用而已,但是如果你要是想实现更多丰富的功能,需要自己写.利用AssetsLibrary.framework,利用这个框架可以获得手 ...
- AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件
AJ分享,必须精品 一:效果 二:封装好的工具条 NYComposeToolbar.h 带代理方法 #import <UIKit/UIKit.h> typedef enum { NYCom ...
随机推荐
- echar图柱状图和折线图混合加双侧y轴坐
代码如下: floorSalesBar(){//方法名====这个方法应该放在methods中并在mounted中调用哦 methods let _this = this; let myChart = ...
- c# 自定义含有标题的容器控件(标题背景为渐变色)
1.控件效果图 此效果图中的标题颜色.字号及字体可以在控件属性中设置.标题背景的渐变色及布局内容的背景色也可以在属性中设置. 2.实现的代码(用户控件) public partial class Uc ...
- Cisco 综合配置(三)
要求: 1.PC1 PC2使用DHCP,获取IP ,VLAN为10 和20,网关在Core Switch 2上2.DHCP和web server VLAN为100,网关在Core Switch 1上3 ...
- vue one one
目录 Vue 渐进式 JavaScript 框架 一.走进Vue 1.what -- 什么是Vue 2.why -- 为什么要学习Vue 3.special -- 特点 4.how -- 如何使用Vu ...
- 第十七周Java实验作业
实验十七 线程同步控制 实验时间 2018-12-10 1.实验目的与要求 (1) 掌握线程同步的概念及实现技术: 多线程并发运行不确定性问题解决方案:引入线程同步机制,使得另一线程使用该方法,就只 ...
- 收藏 | 15 个你非了解不可的 Linux 特殊字符,妈妈再也不用担心我看不懂这些符号了!
不知道大家接触 Linux 系统有多久了,可曾了解过 Linux 中有哪些特殊的字符呢?其实啊,那些特殊字符都大有用处呢,今天的文章就给大家简单地科普一下 Linux 中你需要了解的 15 个特殊字符 ...
- PAT-B 1003. 我要通过!(20) Java版
"答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确&quo ...
- 常见排序算法总结分析之选择排序与归并排序-C#实现
本篇文章对选择排序中的简单选择排序与堆排序,以及常用的归并排序做一个总结分析. 常见排序算法总结分析之交换排序与插入排序-C#实现是排序算法总结系列的首篇文章,包含了一些概念的介绍以及交换排序(冒泡与 ...
- Ubuntu添加新用户并给普通用户赋予root新权限
添加新用户 首先用adduser命令添加普通用户: #adduser newusername 只有在root权限才可以添加新用户 修改密码: #passwd username 赋予root权限 方法1 ...
- 加密解密 Python
常见加密方式和Python实现 1. 前言 我们所说的加密方式,都是对二进制编码的格式进行加密的,对应到Python中,则是我们的Bytes. 所以当我们在Python中进行加密操作的时候,要确保我们 ...