貓貓學iOS 之微博項目實戰(7)程式啟動新特性用UICollectionViewController實現,ios新浪微博項目源碼

來源:互聯網
上載者:User

貓貓學iOS 之微博項目實戰(7)程式啟動新特性用UICollectionViewController實現,ios新浪微博項目源碼

貓貓分享,必須精品

原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址: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 NYNewFeatureControllerstatic 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必須註冊cell3.自訂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

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.