个人相册开发(8)

2019-05-16  本文已影响0人  小石头呢

一.简单搭建个人相册内部界面,用于跳转

1.创建XLPersonPictureController类,用于显示一个相册内部的具体信息

2.在.h文件定义一个变量用于接受外部传递的相册模型

@interface XLPersonPictureController : XLBaseViewController

/**模型来了*/
@property (nonatomic,strong) XLAlbumModel *albumModel;

@end

3.简单的配置界面,添加标题,添加返回按钮,添加编辑。点击编辑按钮弹出工具条。

#import "XLPersonPictureController.h"

@interface XLPersonPictureController ()

@end

@implementation XLPersonPictureController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置标题
    self.title = self.albumModel.name;
    
    //添加返回按钮
    [self addBackItem:nil];
    
    __block typeof(self) weakSelf = self;
    
    //添加编辑按钮
    [self addItemWithName:@"Edit" postion:kButtonPostionTypeRight complish:^(UIButton *item) {
        
        if ([item.titleLabel.text isEqualToString:@"Edit"]) {
            
            //切换button的标题
            [item setTitle:@"Done" forState:UIControlStateNormal];
            
            //创建显示工具条
            [weakSelf showToolBarWithType:ToolBarTypeAlbum handle:^(NSInteger index) {
                
                if (index == 1) {
                    
                    //多选
                }else if(index == 2){
                    
                    //拷贝
                }else if(index == 3){
                    
                    //剪切
                }else{
                    
                    //删除
                }
            }];
            
        }else{
            
            //切换button的标题
            [item setTitle:@"Edit" forState:UIControlStateNormal];
            
            //移除工具条
            [weakSelf hideToolBar];
        }
    }];
}

@end

4.在XLAlbumTableView中didSelectRowAtIndexPath方法中,进行界面跳转

//某个cell被点击了
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //判断编辑状态
    if (self.tableView.editing) {
        
        //编辑状态
        
        //告诉manager某一个被点击了
        [[XLAlbumManager sharedManager] albumEditingStatusChanged:YES index:indexPath.row];
    }else{
        
        //正常状态
        
        //1.取消选中
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        //判断是什么相册
        if (_type == TableViewTypePerson) {
            
            //个人相册
            
            //2.进入下一个界面
            XLPersonPictureController *avc = [XLPersonPictureController new];
            
            //3.传递数据
            avc.albumModel = [XLAlbumManager sharedManager].albumModelsArray[indexPath.row];
            
            //4.找到导航栏控制器
            UINavigationController *nav = (XLNavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            
            //5.跳转
            [nav pushViewController:avc animated:YES];
        }else{
            
            //系统相册
            
        }
    }
}

二.创建显示一个个资源的Cell

1.创建一个XLPictureCell类,并同时创建一个Xib文件,关联控件


2.在EnumConstants中添加一个枚举用于判断要显示什么样的cell

//Cell的类型
typedef NS_ENUM(NSUInteger, CellType) {
    CelllTypeAdd,   //添加
    CellTypeSystem, //系统图片
    CellTypePerson  //个人图片
};

3.在.h文件定义一个变量用于接受应该显示的Cell的类型

//接受外界传递过来的Cell类型
@property (nonatomic,assign) CellType type;

三.创建一个collectionView来承载我们的一个个资源

1.创建一个XLPictureCollectionView类,并提供一个接口用于创建

//类方法创建
+(XLPictureCollectionView *)showCollectionView:(CGRect)frame ViewType:(ViewType)type  Model:(XLAlbumModel *)model toView:(UIView *)superView;

2.遵从协议,设置一些变量用于接受数据

@interface XLPictureCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

/**接受传递的类型*/
@property (nonatomic,assign) ViewType type;

/**接受传递的相册模型*/
@property (nonatomic,strong) XLAlbumModel *albumModel;

/**保存我们显示的资源模型*/
@property (nonatomic,strong) NSMutableArray *pictureModelArray;

/**属性化的collectionView*/
@property (nonatomic,strong) UICollectionView *collectionView;

/**记录编辑状态*/
@property (nonatomic,assign) BOOL isEditing;

@end

3.简单的代建一个界面,先默认返回一个cell,等后续添加数据,再更改

#pragma mark -------类方法创建 ---------
+(XLPictureCollectionView *)showCollectionView:(CGRect)frame ViewType:(ViewType)type Model:(XLAlbumModel *)model toView:(UIView *)superView{
    
    //创建
    XLPictureCollectionView *pcv = [[XLPictureCollectionView alloc] initWithFrame:frame];
    
    //保存类型
    pcv.type = type;
    
    //保存传递过来的相册模型
    pcv.albumModel = model;

    //显示
    [superView addSubview:pcv];
    
    return pcv;
}

#pragma mark -------重写initWithFrame方法 布局 ---------
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        //当前视图的背景颜色
        self.backgroundColor = [UIColor clearColor];
        
        //创建布局对象
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        
        //每一个item的大小
        layout.itemSize = CGSizeMake(kSize, kSize);
        
        //竖向最小间距
        layout.minimumLineSpacing = 1;
        
        //横向最小间距
        layout.minimumInteritemSpacing = 0.001f;
        
        //layout.sectionInset = UIEdgeInsetsMake(5, 5, 5,5);
        
        //创建collectionView
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
        
        //collectionView的背景颜色
        _collectionView.backgroundColor = [UIColor clearColor];
        
        //设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        //显示
        [self addSubview:_collectionView];
        
        //注册
        [_collectionView registerNib:[UINib nibWithNibName:@"XLPictureCell" bundle:nil]  forCellWithReuseIdentifier:@"cellID"];
    }
    return self;
}

#pragma mark -------UICollectionView代理方法 ---------
//返回item的数量
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
   return 1;
}

//每个cell显示什么
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建
    XLPictureCell *cell;
    
    //判断类型
    if (self.type == ViewTypePerson) {
        
        //个人相册
        
        //判断是不是第一个cell被点击了
        if (indexPath.row == 0) {
            
            //初始化cell
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
            
            //cell的类型
            cell.type = CelllTypeAdd;
        }else{
            
        }
    }else{
        
        //系统相册
    }
    return cell;
}

//item被点击了
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //判断类型
    if (self.type == ViewTypePerson) {
        
        //个人相册
        
        //判断是不是第一个被点击了
        if (indexPath.row == 0) {
            
            //显示操作框
            XLActionViewController *myController = [XLActionViewController showActionSheet];
            
            //循环引用
            __block typeof(myController) weakController = myController;
            
            [myController setActionBlock:^(ActionType type) {
                
                if (type == ActionTypeCamera) {
                    
                   //相机来源
                }else{
                    
                   //相册来源
        }else{
            //第一个之外的被点击了
            //判断是不是编辑状态
            if (self.isEditing) {
                //编辑状态
                }else{

                }
            }else{     
                //进入浏览界面
            }
        }
    }else{
        //系统相册    
}

@end
上一篇 下一篇

猜你喜欢

热点阅读