猫猫学iOS 之微博项目实战(7)程序启动新特性用UICollectionViewController实现

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243

一:效果

这里实现了大多数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
### 回答1: 我不太清楚如何用Java来实现猫猫小游戏,但是可以提供一些建议:首先,需要创建一个类来表示猫,然后定义一些方法来控制它的移动;其次,可以使用Java的GUI功能来创建游戏的用户界面;最后,可以使用Java的线程类来实现猫的自动移动和游戏的计时。 ### 回答2: 躲猫猫是一款非常经典的小游戏,让我们使用Java来实现吧! 首先,我们需要创建一个游戏窗口,可以使用Java Swing库来实现。在窗口中,我们可以添加一个游戏地图,猫咪和玩家。 游戏地图可以是一个清空的二维数组,其中0表示空格,1表示墙壁,2表示猫咪,3表示玩家。我们可以在构造函数中初始化地图并随机放置猫咪和玩家。 然后,我们可以使用键盘监听器来监听玩家的上下左右移动指令,并在地图上更新玩家的位置。玩家每次移动后,我们需要检查是否成功躲开了猫咪。 在检查躲猫猫的过程中,我们可以使用深度优先搜索算法来判断猫咪是否可以到达玩家的位置。如果玩家和猫咪之间没有路径,那么玩家就成功躲过了猫咪。 最后,我们可以在游戏窗口中显示游戏地图,并根据玩家的移动更新地图上的猫咪和玩家的位置。 实现猫猫小游戏可以提高我们的编程能力和逻辑思维能力,同时也很有趣。希望以上的解答对您有帮助! ### 回答3: 躲猫猫小游戏是一款常见的游戏玩法,需要通过编程实现其中的逻辑。在Java中,我们可以通过面向对象的思想来设计和实现这个游戏。 首先,我们需要创建一个Cat类和一个Mouse类。Cat类表示猫的属性和行为,Mouse类表示老鼠的属性和行为。在这两个类中,我们可以定义猫和老鼠的初始位置,以及它们的移动方式。 接着,我们可以创建一个Game类,用于处理游戏的逻辑。在游戏开始时,先创建一个地图,并随机生成猫和老鼠的初始位置。然后,循环进行以下操作:首先,根据输入的方向键来移动猫和老鼠的位置;然后,判断猫是否追到了老鼠或者老鼠成功逃脱,以决定游戏是否结束;最后,根据游戏结果输出相应的消息,并问玩家是否继续游戏。 为了实现游戏界面的交互,我们可以使用Java图形界面库,如Swing或JavaFX。在游戏界面上,我们可以显示地图和猫、老鼠的位置,同时提供方向键控制猫的移动。 最后,为了使游戏更加有趣,我们还可以添加额外的功能,如设置障碍物,增加关卡难度等。 总之,通过使用Java编程语言,我们可以轻松实现猫猫小游戏。这个游戏不仅可以让玩家感受到编程的乐趣,还可以锻炼玩家的逻辑思维和操作能力。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值