Cat learning iOS Weibo Project Practice (7) program startup new features implemented with UICollectionViewController, ios Sina Weibo project source code
CAT/CAT sharing, must be excellent
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Address: http://blog.csdn.net/u013357243
I. Effect
The new features of software are implemented in most apps. They are implemented using UICollectionViewController.
Ii. Ideas
UICollectionViewController is used here to implement a UICollectionViewController with no gaps. Each cell is a screen-based UICollectionViewController, Which is customized. Then, put the following UIPageControl and the start and share buttons on the last page.
When calling the SDK, obtain the current app version and the previous version. Compare the two versions. The current version is different from the previous version. The current version is obtained from infoDictionary, and the previous version is obtained from the NYVersionKey in the NSUserDefaults, in addition, Apple allows you to upload apps earlier than the current version. If the app is in the first version, the previous version has no value and will be different.
Set different root controllers of window Based on the result.
To customize UICollectionViewController, follow these steps:
Note:
1. Set layout parameters during initialization
2. collertionView must register cell
3. Custom cell
Iii. Code
Call part of the Code: AppDelegate
// 1. Obtain the current version NSString * currentVersion = [NSBundle mainBundle]. infoDictionary [@ "CFBundleVersion"]; // 2. Obtain the last version number NSString * lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey: NYVersionKey]; NYLog (@ "currentVersion =%@, lastVersion =%@", currentVersion, lastVersion); // determine whether a new version is available if ([currentVersion is1_tostring: lastVersion]) {// if there is no new version (the current version is different from the previous version, the current version is obtained from infoDictionary, and the previous version is It is obtained from the NYVersionKey in your NSUserDefaults and Apple allows you to upload an app earlier than the current version. If it is the first version, it will be different if it has no value in the previous version .) // Create tabBarVC NYTabBarController * tabBarVC = [[NYTabBarController alloc] init]; // set the window and controller self. window. rootViewController = tabBarVC;} else {// if a new version exists, // enter the new feature page NYNewFeatureController * newFeatureVC = [[NYNewFeatureController alloc] init]; newFeatureVC. view. backgroundColor = [UIColor redColor]; self. window. rootViewController = newFeatureVC; // use preference settings to save the current version. [[NSUserDefaults standardUserDefaults] setObject: currentVersion forKey: NYVersionKey];}
Custom collectionViewController
NYNewFeatureController. m
/// NYNewFeatureController. m // cat Weibo /// 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]; // register the cell. By default, this type of cell [self. collectionView registerClass: [NYNewFeatureCell class] forCellWithReuseIdentifier: reuseIdentifier]; // page self. collectionView. pagingEnabled = YES; // cancel the spring effect self. collectionView. bounces = NO; // the scroll bar self is not displayed. collectionView. showsHorizontalScrollIndicator = NO; // Add pageController [self setUpPageControl]; // Do any additional setup after loading the view .} // Add pageController-(void) setUpPageControl {// Add pageController. You only need to set the location. You do not need to manage the dimension UIPageControl * control = [[UIPageControl alloc] init]; control. numberOfPages = 4; control. pageIndicatorTintColor = [UIColor blackColor]; control. currentPageIndicatorTintColor = [UIColor redColor]; // set center control. center = CGPointMake (self. view. width * 0.5, self. view. height); _ control = control; [self. view addSubview: control];}/* Note the following when using UICollectionViewController: 1. set layout parameters during initialization 2. collertionView must register cell3. custom cell */-(instancetype) init {// UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init]; // set the cell size layout. itemSize = [UIScreen mainScreen]. bounds. size; // clear the layout line spacing of the cell interval. minimumLineSpacing = 0; // set the sliding direction of the cell to layout. scrollDirection = require; return [super initWithCollectionViewLayout: layout];}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark-UICollectionView proxy and Data Source-(NSInteger) numberOfSectionsInCollectionView :( UICollectionView *) collectionView {return 1;}-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section {return 4;}-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath {// dequeueReusableCellWithReuseIdentifier/1. first, fetch cell/2 from the cache pool. check whether Cell is currently registered. If cell is registered, it will help you create cell // 3. if the image is not registered, the following error occurs: NYNewFeatureCell * cell = [collectionView failed: reuseIdentifier forIndexPath: indexPath]; // spliced image name 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 proxy // call-(void) scrollViewDidScroll (UIScrollView *) scrollView {// obtain the current offset, calculate the current page int page = scrollView. contentOffset. x/scrollView. bounds. size. width + 0.5; // set page number _ control. currentPage = page;} @ end
Custom cell
NYNewFeatureCell. h
/// NYNewFeatureCell. h // cat Weibo /// 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; // determine whether it is the last page-(void) setIndexPath :( NSIndexPath *) indexPath count :( int) count; @ end
NYNewFeatureCell. m
/// NYNewFeatureCell. m // cat Weibo /// 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; // share button @ property (nonatomic, weak) UIButton * shareButton; // start @ property (nonatomic, weak) UIButton * startButton; @ end @ implementation NYNewFeatureCell // The sharing button is loaded in a lazy manner-(UIButton *) shareButton {if (_ shareButton = nil) {UIButton * btn = [UIButton buttonWithType: Hangzhou]; [btn setTitle: @ "share with everyone" forState: UIControlStateNormal]; [btn setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateNormal]; [btn setImage: [UIImage imageNamed: @ "success"] forState: UIControlStateSelected]; [btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; [btn sizeToFit]; [self. contentView addSubview: btn]; _ shareButton = btn;} return _ shareButton;} // the start button for lazy loading-(UIButton *) startButton {if (_ startButton = nil) {UIButton * startBtn = [UIButton buttonWithType: Hangzhou]; [startBtn setTitle: @ "" forState: UIControlStateNormal]; [startBtn setBackgroundImage: [UIImage imageNamed: @ "unknown"] forState: UIControlStateNormal]; [startBtn setBackgroundImage: [UIImage imageNamed: @ "unknown"] forState: Unknown]; [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; // Note: Be sure to load contentView [self. contentView addSubview: imageV];} return _ imageView;} // frame-(void) layoutSubviews {[super layoutSubviews]; self. imageView. frame = self. bounds; // The share button self. shareButton. center = CGPointMake (self. width * 0.5, self. height * 0.8); // the start button self. startButton. center = CGPointMake (self. width * 0.5, self. height * 0.9);}-(void) setImage :( UIImage *) image {_ image = image; self. imageView. image = image;} // determines whether the current cell is the last page-(void) setIndexPath :( NSIndexPath *) indexPath count :( int) count {if (indexPath. row = count-1) {// The last page. The share and start buttons are displayed. shareButton. hidden = NO; self. startButton. hidden = NO;} else {// not the last page. Hide the share and start buttons. shareButton. hidden = YES; self. startButton. hidden = YES ;}/// call-(void) start when you click start Weibo {// enter tabBarVc NYTabBarController * tabBarVc = [[NYTabBarController alloc] init]; // switch the root Controller: You can directly clear the previous root controller NYKeyWindow. rootViewController = tabBarVc;} @ end
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.