课后族项目实战(2)-新特性的实现(使用UICollectionView)

来源:互联网 发布:从u盘启动linux系统 编辑:程序博客网 时间:2024/06/06 17:53
1.新特性的显示
          在程序第一次启动或者更新到最新版本第一次启动时展示。
2.如何判断?
          可以在沙盒中存一个老版本的版本号,与当前版本号进行比较,如果不一样,就显示新特性界面。
3.实现方式
          使用UICollectionView。只一行cell,每一个cell的大小就是整个屏幕的大小
          *为collection cell设置布局,配置collectionview相关属性
-(instancetype)init{     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];     layout.minimumLineSpacing = 0;     layout.itemSize = [UIScreen mainScreen].bounds.size;     layout.scrollDirection =     UICollectionViewScrollDirectionHorizontal;     return [self initWithCollectionViewLayout:layout];}- (void)viewDidLoad {    [super viewDidLoad];    self.collectionView.pagingEnabled = YES;//设置分页    self.collectionView.bounces = NO;//滑倒边界不反弹    self.collectionView.showsHorizontalScrollIndicator = NO;//不显示横向滚动条    [self.collectionView registerClass:[TBNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];//注册cell}
 *设置collectionview的section为1,section里面的item的个数视新特性的页数决定,我这里有3页,就设置成3
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {    return 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {    return 3;}
          *最后为每一个cell添加图片
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    TBNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];    NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld",indexPath.row + 1];    cell.image = [UIImage imageNamed:imageName];    return cell;}
          效果如下:

          *附上TBNewFeatureCell的代码:
          TBNewFeatureCell.h
#import <UIKit/UIKit.h>@interface TBNewFeatureCell : UICollectionViewCell@property (nonatomic,strong) UIImage *image;@end
           TBNewFeatureCell.m
#import "TBNewFeatureCell.h"@interface TBNewFeatureCell ()@property (nonatomic, weak) UIImageView *imageView;@end@implementation TBNewFeatureCell-(UIImageView *)imageView{    if (_imageView == nil) {        UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.bounds];        imageV.image = _image;        _imageView = imageV;        [self.contentView addSubview:imageV];    }    return _imageView;}-(void)setImage:(UIImage *)image{    _image = image;    self.imageView.image = image;}@end

0 0
原创粉丝点击