美文网首页
个人相册开发(7)

个人相册开发(7)

作者: 小石头呢 | 来源:发表于2019-05-15 09:06 被阅读0次

一.创建一个类,用于承载显示的一个个Cell

1.创建XLAlbumTableView类,并在EnumConstants中加入 ViewType枚举

//View的类型
typedef NS_ENUM(NSInteger, ViewType) {
    ViewTypeSystem, //系统相册
    ViewTypePerson  //个人相册
};

2.在.h文件里面,提供一个接口用于创建该类,并且对于个人相册界面,需要进入编辑状态,所以我们需要定义一个变量用于接受外部的编辑状态,并提供全选接口

/**接受tableView的状态*/
@property (nonatomic,assign) BOOL isEditing;

//创建显示tableView的UIView
+(XLAlbumTableView *)showAlbumView:(CGRect)frame ViewType:(TableViewType)type Array:(NSMutableArray *)modelsArray superView:(UIView *)sView;

//全选-界面上全选
-(void)selectAllAlbum;

3.在.m文件里面,需要定义一个全局的UITableView,并定义两个变量接受我们的相关数据

/**全局属性化的tableView*/
@property (nonatomic,strong) UITableView *tableView;

/**接受我们的类型*/
@property (nonatomic,assign) TableViewType type;

/**接受传递过来的数据模型组*/
@property (nonatomic,strong) NSMutableArray *modelsArray;

4.类方法创建的实现

#pragma mark -------类方法创建 ---------
+(XLAlbumTableView *)showAlbumView:(CGRect)frame ViewType:(TableViewType)type Array:(NSMutableArray *)modelsArray superView:(UIView *)sView{
    
    //创建
    XLAlbumTableView *atv = [[XLAlbumTableView alloc] initWithFrame:frame];
    
    //保存类型
    atv.type = type;
    
    //保存数据
    atv.modelsArray = modelsArray;
    
    //NSLog(@"1");
    //打印结果显示先来到这里才会配置cell,不会出现type还没有传递好的情况
    
    //显示
    [sView addSubview:atv];
    
    return atv;
}

#pragma mark -------重写initWithFrame方法 布局 ---------
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        //创建tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        //设置代理
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        //去除纵向滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        
        //设置分割线风格
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //编辑模式下是否允许多选
        _tableView.allowsMultipleSelectionDuringEditing = YES;
        
        //显示
        [self addSubview:_tableView];
        
        //注册cell
        [_tableView registerNib:[UINib nibWithNibName:@"XLAlbumCell" bundle:nil] forCellReuseIdentifier:@"cellID"];
        
    }
    return self;
}

5.重写type的set方法判断是否需要监听数据源改变了的消息和消息的移除,以及重写isEditing的set方法判断是否需要进入编辑状态

#pragma mark -------重写type的set方法 ---------
-(void)setType:(TableViewType)type{
    _type = type;
    
    if (type == TableViewTypePerson) {
        
        //监听数据源改变的消息
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataSourceChanged) name:kAlbumModelsArrarChangeNotificationName object:nil];
    }
}

#pragma mark -------移除消息 ---------
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -------重写isEditing的set方法 ---------
-(void)setIsEditing:(BOOL)isEditing{
    
    _isEditing = isEditing;
    
    //设置tableView的编辑状态
    [self.tableView setEditing:isEditing animated:YES];
}

6.UITableView的代理方法的实现

#pragma mark -------UITableView ---------
//配置有多少个相册
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelsArray.count;
}

//配置每个相册具体显示什么内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //创建cell
    XLAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    
    //传递数据
    cell.model = self.modelsArray[indexPath.row];
    
    //判断是不是个人相册
    if (self.type == TableViewTypePerson) {
        
        //长按手势
        UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGRDidSelected:)];
        [cell addGestureRecognizer:longGR];
    }
    
    return cell;
}

//配置cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}

//某个cell被点击了
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //判断编辑状态
    if (self.tableView.editing) {
        
        //编辑状态
        
        //告诉manager某一个被点击了
        [[XLAlbumManager sharedManager] albumEditingStatusChanged:YES index:indexPath.row];
    }else{
        
        //正常状态 界面跳转
    }
}

//某个cell取消选中了
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //判断是不是编辑状态
    if (self.tableView.editing) {
        
        //告诉manager某一个取消点击了
        [[XLAlbumManager sharedManager] albumEditingStatusChanged:NO index:indexPath.row];
    }
}

//设置tableView的编辑状态 左边显示的样式 delete insert none
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //可以勾选的样式
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}

7.cell正常状态的长按时间以及编辑状态的全选方法的实现

#pragma mark -------cell上的长按手势事件 ---------
-(void)longGRDidSelected:(UILongPressGestureRecognizer *)sender{
    
    //长按结束情况下
    if (sender.state == UIGestureRecognizerStateEnded) {
        
        //获得手势添加的cell
        XLAlbumCell *cell = (XLAlbumCell *)sender.view;
        
        //重命名
        XLActionViewController *myController = [XLActionViewController showAlertControllerWithType:AlertTypeAddAlbum AndName:@"请输入新的相册名"];
        
        //解决循环引用的可能
        __block typeof(myController) weakController = myController;
        
        //确定按钮点击做什么
        [myController setSureblock:^(NSString *albumName) {
            
            //NSLog(@"%@ %@",cell.model.name,albumName);
            
            //创建相册
            //判断相册是否存在
            BOOL result = [[XLAlbumManager sharedManager] renameAlbum:albumName WithOldName:cell.model.name];
            
            //如果创建失败
            if (result == NO) {
                
                //重名
                XLActionViewController *myAlert = [XLActionViewController showAlertControllerWithType:AlertTypeAlert AndName:@"相册已存在"];
                
                //解决循环引用的可能
                __block typeof(myAlert) weakAlert = myAlert;
                
                //确定按钮点击做的事
                [myAlert setCancelblock:^{
                    [weakAlert dismiss];
                }];
            }
            
            //移除
            [weakController dismiss];
        }];
        
        //删除按钮点击做什么
        [myController setCancelblock:^{
            
            //移除
            [weakController dismiss];
        }];
        
        
    }
}

#pragma mark -------全选 ---------
-(void)selectAllAlbum{
    
    //判断是全选还是取消全选
    if ([[XLAlbumManager sharedManager] isAllSelected]) {
        
        //已经全选了
        for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
            
            //界面取消全选
            [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
            
            //数据源取消全选
            [[XLAlbumManager sharedManager] albumEditingStatusChanged:NO index:indexPath.row];
        }
    }else{
        
        //需要全选
        
        //界面全选
        for (int i = 0; i < [XLAlbumManager sharedManager].albumModelsArray.count; i++) {
            
            //获取i对应的row的indexPath
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            
            //判断当前的indexPath对应的cell有没有被选中
            if ([self.tableView.indexPathsForSelectedRows containsObject:indexPath] == NO) {
                
                [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            }
        }
        
        //数据源全选
        [[XLAlbumManager sharedManager] selectAllAlbum];
    }
}

二.完善我们的个人相册界面

1.将我们刚才创建的类XLAlbumTableView属性化,添加一个变量用于判定添加按钮是否可以点击,因为添加按钮需要判断是否可以使用,所以我们将其属性化

/**属性化的XLAlbumTableView*/
@property (nonatomic,strong) XLAlbumTableView *tableView;

/**是否可以添加相册*/
@property (nonatomic,assign) BOOL addStatus;

/**add按钮*/
@property (nonatomic,strong) UIBarButtonItem *addItem;

2.点击编辑按钮,进入编辑状态,弹出工具条,并根据工具条回调的信息做相应的事件。点击添加按钮,弹出对话框添加相册

#pragma mark -------界面加载 ---------
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加logo
    [self addLogo];
    
    //避免循环引用
    __block typeof(self) weakSelf = self;
    
    //添加左边的按钮
    [self addItemWithName:@"Edit" postion:kButtonPostionTypeLeft complish:^(UIButton *item) {
      
        //Edit按钮被点击了
        if ([item.titleLabel.text isEqualToString:@"Edit"]) {
            
            //切换button的标题
            [item setTitle:@"Done" forState:UIControlStateNormal];
            
            //传递tableView的状态
            weakSelf.tableView.isEditing = YES;
            
            //add按钮禁用
            weakSelf.addStatus = NO;
            
            //添加工具条
            [weakSelf showToolBarWithType:ToolBarTypeMain handle:^(NSInteger index) {
                
                //按钮被点击需要做什么事情
                if (index == 1) {
                    
                    //全选
                    [weakSelf.tableView selectAllAlbum];
                    
                }else{
                    
                    //删除
                    [[XLAlbumManager sharedManager] deleteAlbum];
                }
            }];
            
        }else{
            
            //切换button的标题
            [item setTitle:@"Edit" forState:UIControlStateNormal];
            
            //传递tableView的状态
            weakSelf.tableView.isEditing = NO;
            
            //add按钮接触禁用
            weakSelf.addStatus = YES;
            
            //移除工具条
            [weakSelf hideToolBar];
        }
        
    }];
    
    //添加右边的按钮
    self.addItem = [self addItemWithName:@"Add" postion:kButtonPostionTypeRight complish:^(UIButton *item) {
        
        //创建
        XLActionViewController *myController = [XLActionViewController showAlertControllerWithType:AlertTypeAddAlbum AndName:@"请输入相册名称"];
        
        //解决循环引用的可能
        __block typeof(myController) weakController = myController;
        
        //SureBlock回调做的事
        [myController setSureblock:^(NSString *albumName) {
            
            //NSLog(@"%@",albumName);
            
            //创建相册
            //判断相册是否存在
            BOOL result = [[XLAlbumManager sharedManager] createAlbumWithName:albumName];
            
            //NSLog(@"%d",result);
            
            //如果创建失败
            if (result == NO) {
                
                //重名
                XLActionViewController *myAlert = [XLActionViewController showAlertControllerWithType:AlertTypeAlert AndName:@"相册已存在"];
                
                //解决循环引用的可能
                __block typeof(myAlert) weakAlert = myAlert;
                
                //移除
                [myAlert setCancelblock:^{
                    [weakAlert dismiss];
                }];
            }
            
            //移除
            [weakController dismiss];
        }];
        
        //cancelBlock回调做的事
        [myController setCancelblock:^{
            
            //移除
            [weakController dismiss];
        }];
    }];
    
    //创建含有tableView的视图
    self.tableView = [XLAlbumTableView showAlbumView:CGRectMake(0, self.navigationController.navigationBar.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-self.navigationController.navigationBar.bottom) ViewType:TableViewTypePerson Array:[XLAlbumManager sharedManager].albumModelsArray superView:self.view];
}

#pragma mark -------重写addStatus的set方法判断添加按钮是否被禁用 ---------
-(void)setAddStatus:(BOOL)addStatus{
    _addStatus = addStatus;
    
    if (addStatus == YES) {
        
        //禁用
        self.addItem.enabled = YES;
    }else{
        
        //解除禁用
        self.addItem.enabled = NO;
    }
}

相关文章

  • 个人相册开发(7)

    一.创建一个类,用于承载显示的一个个Cell 1.创建XLAlbumTableView类,并在EnumConsta...

  • 个人相册开发(12)

    一.个人相册内部界面的具体显示 添加两个消息,一个用来监听数据源是否改变,一个用来监听编辑状态。数据源改变了,需要...

  • 个人相册开发(11)

    一.回调系统相册被选择的资源 1.因为我们通过单例模式创建XLSystemAlbumManager,所以它会在整个...

  • 个人相册开发(13)

    一.创建浏览界面的Cell 1.创建XLPictureShowCell类,并同时创建Xib文件 2.Xib文件布局...

  • 个人相册开发(10)

    一.写一个类用于显示相机和系统相册界面 在plist文件设置相应的权限框文本 1.创建一个XLImagePicke...

  • 个人相册开发(9)

    一.类似个人相册界面的系统相册界面 1.创建系统相册的界面XLSystemAlbumController类,利用父...

  • 个人相册开发(8)

    一.简单搭建个人相册内部界面,用于跳转 1.创建XLPersonPictureController类,用于显示一个...

  • 实现悬浮于上一个Activity背景上的Activity

    在开发中,经常会遇到诸如,点击个人相册中的某张图片,然后弹出悬浮显示这张图片,并可以对这张图片进行缩放等操作。很多...

  • 个人相册

  • 开发使用 iBeacon 的 iOS 7 应用

    开发使用 iBeacon 的 iOS 7 应用 开发使用 iBeacon 的 iOS 7 应用

网友评论

      本文标题:个人相册开发(7)

      本文链接:https://www.haomeiwen.com/subject/mxhraqtx.html