2018-06-01坑爹的ManageCollection

2018-06-01  本文已影响16人  香蕉你个菠萝

ManageCollectViewController.h

#import "ViewBaseController.h"

@interface ManageCollectViewController : ViewBaseController
@property (nonatomic,strong) NSMutableArray *preDataArray;//前一页
@end

ManageCollectViewController.m

//
//  ManageCollectViewController.m
//  DXYiGe
//
//  Created by JHT on 2018/5/31.
//  Copyright © 2018年 QC. All rights reserved.
//

#import "ManageCollectViewController.h"
#import "ManageCollectMainView.h"
#import "ManageCollectViewModel.h"

@interface ManageCollectViewController ()
@property (nonatomic,strong) ManageCollectMainView *mainView;
@property (nonatomic,strong) ManageCollectViewModel *viewModel;
@end

@implementation ManageCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
-(void)bindViewModel {
    
}
-(void)setPreDataArray:(NSMutableArray *)preDataArray {
    
    self.mainView.preDataArray = preDataArray;
    
}
-(void)editBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSString * isHidden = [NSString stringWithFormat:@"%@",sender.selected ? @"1":@"0"];
    [self.viewModel.editSubject sendNext:isHidden];
    [self.mainView setIsHiddenBtn:sender.selected];
}
-(UIBarButtonItem *)rightButton {
    UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 43, 23)];
    [btn setTitle:@"编辑" forState:UIControlStateNormal];
    [btn setTitle:@"完成" forState:UIControlStateSelected];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setTitleColor:DEFAULT_YELLOW_COLOR forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [btn setBackgroundColor:[UIColor clearColor]];
    [btn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];//设置按钮的点击事件
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

-(void)addChildView {
    [self.view addSubview:self.mainView];
}
-(void)updateViewConstraints {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [super updateViewConstraints];
}
-(ManageCollectMainView *)mainView {
    if (!_mainView) {
        _mainView = [[ManageCollectMainView alloc] init];
    }
    return _mainView;
}
-(ManageCollectViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[ManageCollectViewModel alloc] init];
    }
    return _viewModel;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

ManageCollectMainView.h

#import "BaseView.h"

@interface ManageCollectMainView : BaseView
@property (nonatomic,strong) NSMutableArray *topDataArray;
@property (nonatomic,strong) NSMutableArray *bottomDataArray;

@property (nonatomic,strong) NSMutableArray *preDataArray;//传入

@property (nonatomic,assign) BOOL isHiddenBtn;
@end

ManageCollectMainView.m

//
//  ManageCollectMainView.m
//  DXYiGe
//
//  Created by JHT on 2018/5/31.
//  Copyright © 2018年 QC. All rights reserved.
//

#import "ManageCollectMainView.h"
#import "ManageCollectViewModel.h"
#import "ManageCollectionCell.h"
#import "ManageCollectItemModel.h"

@interface ManageCollectMainView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) ManageCollectViewModel *viewModel;
@property (nonatomic,strong) UICollectionView *topCollectionView;
@property (nonatomic,strong) UICollectionView *bottomCollectionView;
@property (nonatomic,strong) NSMutableArray *allDataArray;

@end

@implementation ManageCollectMainView

-(instancetype)initWithViewModel:(id<BaseViewModelProtocol>)viewModel {
    self.viewModel = (ManageCollectViewModel *)viewModel;
    return [super initWithViewModel:self.viewModel];
}
-(void)setIsHiddenBtn:(BOOL)isHiddenBtn {
    _isHiddenBtn = isHiddenBtn;
    NSLog(@"main-ishidden->%@",isHiddenBtn ? @"1":@"0");//edit 完成
    [self.topCollectionView reloadData];
    [self.bottomCollectionView reloadData];
    
    

}

-(void)bindViewModel {
    


    [self.viewModel.editSubject subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"main-ishidden->%@",x);
    }];
    
}

-(void)setPreDataArray:(NSMutableArray *)preDataArray {//5
    
    NSMutableArray * top = [NSMutableArray array];
    NSMutableArray * bottom = [NSMutableArray array];
    
    NSMutableArray *selectItemArray = [NSMutableArray array];//未中
    
    [preDataArray enumerateObjectsUsingBlock:^(ManageCollectItemModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (obj.selected) {
            [top addObject:obj];
            [selectItemArray addObject:[NSString stringWithFormat:@"%@",obj.id]];
        }
        
    }];
    
    NSLog(@"all---->%@\n 1-5->%@",self.allDataArray,selectItemArray);
    
    //去重
    for (int i = 0; i < self.allDataArray.count; i++) {
        BOOL isrepeat = NO;
        ManageCollectItemModel * model = self.allDataArray[i];
        
        for (int j = 0; j<selectItemArray.count; j++) {
            NSString * idStr = selectItemArray[j];
            if ([idStr isEqualToString:model.id]) {
                isrepeat = YES;
                break;
            }
        }
        if (!isrepeat) {
            [bottom addObject:model];
        }
    }
    

    
   
    
    NSLog(@" top->%@\n  bottom->%@",top,bottom);
    self.topDataArray = top;
    self.bottomDataArray = bottom;
    [self.topCollectionView reloadData];
    [self.bottomCollectionView reloadData];
    
}

-(NSMutableArray *)allDataArray {
    if (!_allDataArray) {
        _allDataArray = [NSMutableArray array];
        //设置数据源 all
        for (int i = 0; i<7; i++) {
            ManageCollectItemModel * itemModel = [[ManageCollectItemModel alloc] init];
            itemModel.id = [NSString stringWithFormat:@"%d",i];
            itemModel.selected = NO;
            itemModel.content = [NSString stringWithFormat:@"i am %d",i];
            [_allDataArray addObject:itemModel];
        }
        
    }
    return _allDataArray;
}
-(NSMutableArray *)bottomDataArray {
    if (!_bottomDataArray) {
        _bottomDataArray = [NSMutableArray array];
    }
    return _bottomDataArray;
}
-(NSMutableArray *)topDataArray {
    if (!_topDataArray) {
        _topDataArray = [NSMutableArray array];
    }
    return _topDataArray;
}
# pragma mark collectionView代理事件
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([collectionView isEqual:self.topCollectionView]) {
        return self.topDataArray.count;
    }else {
        return self.bottomDataArray.count;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ManageCollectionCell  *cell =[collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([ManageCollectionCell class])] forIndexPath:indexPath];
    if ([collectionView isEqual:self.topCollectionView]) {
        
        ManageCollectItemModel * model = [ManageCollectItemModel mj_objectWithKeyValues:self.topDataArray[indexPath.row]];
        cell.itemModel = model;
        if (!self.isHiddenBtn) {
            cell.editBtn.hidden = YES;
        }else cell.editBtn.hidden = NO;
        
        [cell.editBtn setBackgroundImage:[UIImage imageNamed:@"del"] forState:UIControlStateNormal];
        
        WS(weakSelf)
        cell.editBlock = ^(ManageCollectItemModel *model) {
            
            NSLog(@"上面dele下面add--%@",model);
            
            model.selected = NO;
            [self.bottomDataArray addObject:model];
            [weakSelf.bottomCollectionView reloadData];
            
            for (int i = 0; i< self.topDataArray.count; i++) {
                ManageCollectItemModel * tempModel = self.topDataArray[i];
                if ([model.id isEqualToString:tempModel.id]) {
                    [self.topDataArray removeObjectAtIndex:i];
                }
            }
            
            [weakSelf.topCollectionView reloadData];
        };
        
        return cell;
        
    }else {
        
        ManageCollectItemModel * model = [ManageCollectItemModel mj_objectWithKeyValues:self.bottomDataArray[indexPath.row]];
        cell.itemModel = model;
        if (!self.isHiddenBtn) {
            cell.editBtn.hidden = YES;
        }else cell.editBtn.hidden = NO;
        
        [cell.editBtn setBackgroundImage:[UIImage imageNamed:@"pay_xuanzhogn"] forState:UIControlStateNormal];
        
        WS(weakSelf)
        
        cell.editBlock = ^(ManageCollectItemModel *model) {
            
            NSLog(@"点击的itemBtn--%@",model);
            
            model.selected = YES;
            [self.topDataArray addObject:model];
            [weakSelf.topCollectionView reloadData];
            
            for (int i = 0; i< self.bottomDataArray.count; i++) {
                ManageCollectItemModel * tempModel = self.bottomDataArray[i];
                if ([model.id isEqualToString:tempModel.id]) {
                    [self.bottomDataArray removeObjectAtIndex:i];
                }
            }
            
            [weakSelf.bottomCollectionView reloadData];
        };
        
        return cell;
    }
    
}
#pragma mark 图片点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(150, 50);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(15, 15, 15, 15);
}
//layout协议
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    return 7.f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    return 7.f;
}

//允许移动
-(BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isHiddenBtn) {
        return YES;
    }else return NO;
}

-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    if ([collectionView isEqual:self.topCollectionView]) {
        //获取
        id object = [self.topDataArray objectAtIndex:sourceIndexPath.item];
        //移除
        [self.topDataArray removeObject:object];
        //插入
        [self.topDataArray insertObject:object atIndex:destinationIndexPath.item];

    }
}
-(void)handleLongGesture:(UILongPressGestureRecognizer *)longGesture {
    
    switch (longGesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            //判断手势点是否在路径上
            NSIndexPath * indexPath = [self.topCollectionView indexPathForItemAtPoint:[longGesture locationInView:self.topCollectionView]];
            if (indexPath == nil) {
                break;
            }
            [self.topCollectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
            
            for (ManageCollectionCell *cell in [self.topCollectionView visibleCells]) {
                [self startShake:cell];
            }
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            [self.topCollectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.topCollectionView]];
        
            
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [self.topCollectionView endInteractiveMovement];
            
            for (ManageCollectionCell *cell in [self.topCollectionView visibleCells]) {
                [self stopShake:cell];
            }
        }
            break;
            
        default:
            [self.topCollectionView cancelInteractiveMovement];
            break;
    }
}

-(void)startShake:(ManageCollectionCell *)cell {
    
    CAKeyframeAnimation * keyAnimation = [CAKeyframeAnimation animation];
    keyAnimation.keyPath = @"transform.rotation";
    keyAnimation.values = @[@(-2 / 180.0 * M_PI),@(2 /180.0 * M_PI),@(-2/ 180.0 * M_PI)];//度数转弧度
    keyAnimation.removedOnCompletion = NO;
    keyAnimation.fillMode = kCAFillModeForwards;
    keyAnimation.duration = 0.3;
    keyAnimation.repeatCount = MAXFLOAT;
    [cell.layer addAnimation:keyAnimation forKey:@"cellShake"];
    
    
}

-(void)stopShake:(ManageCollectionCell *)cell {
    [cell.layer removeAnimationForKey:@"cellShake"];
}

-(UICollectionView *)topCollectionView {
    if (!_topCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _topCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        [layout setItemSize:CGSizeMake(150, 50)];//100
        
        _topCollectionView.delegate = self;
        _topCollectionView.dataSource = self;
        _topCollectionView.pagingEnabled = NO;
        _topCollectionView.showsHorizontalScrollIndicator = NO;
        _topCollectionView.backgroundColor = RGB(240, 240, 240);
        [_topCollectionView registerClass:[ManageCollectionCell class] forCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([ManageCollectionCell class])]];
        
        UILongPressGestureRecognizer * longGest = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongGesture:)];
        [_topCollectionView addGestureRecognizer:longGest];
    }
    return _topCollectionView;
}
-(UICollectionView *)bottomCollectionView {
    if (!_bottomCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _bottomCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        [layout setItemSize:CGSizeMake(150, 50)];//100
        
        _bottomCollectionView.delegate = self;
        _bottomCollectionView.dataSource = self;
        _bottomCollectionView.pagingEnabled = NO;
        _bottomCollectionView.showsHorizontalScrollIndicator = NO;
        _bottomCollectionView.backgroundColor = RGB(240, 240, 240);
        [_bottomCollectionView registerClass:[ManageCollectionCell class] forCellWithReuseIdentifier:[NSString stringWithUTF8String:object_getClassName([ManageCollectionCell class])]];
    }
    return _bottomCollectionView;
}

-(void)setupViews {
    self.isHiddenBtn = NO;
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.topCollectionView];
    [self addSubview:self.bottomCollectionView];
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
}
-(void)updateConstraints {
    [self.topCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(20);
        make.size.mas_offset(CGSizeMake(SCREEN_WIDTH-40, 200));
    }];
    
    [self.bottomCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topCollectionView.mas_bottom).offset(20);
        make.left.equalTo(self).offset(20);
        make.size.mas_offset(CGSizeMake(SCREEN_WIDTH-40, 200));
    }];
    [super updateConstraints];
}
- (ManageCollectViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[ManageCollectViewModel alloc] init];
    }
    return _viewModel;
}

@end

ManageCollectionCell.h


#import <UIKit/UIKit.h>
#import "ManageCollectItemModel.h"
@interface ManageCollectionCell : UICollectionViewCell
@property (nonatomic,strong) ManageCollectItemModel *itemModel;
@property (nonatomic,strong) UILabel *contentLabel;
@property (nonatomic,strong) UIButton *editBtn;
@property (nonatomic,copy) void (^editBlock) (ManageCollectItemModel * model);

@end

ManageCollectionCell.m

//
//  ManageCollectionCell.m
//  DXYiGe
//
//  Created by JHT on 2018/5/31.
//  Copyright © 2018年 QC. All rights reserved.
//

#import "ManageCollectionCell.h"

@interface ManageCollectionCell ()
@end

@implementation ManageCollectionCell

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.contentLabel];
        [self addSubview:self.editBtn];
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
    }
    return self;
}
-(void)setItemModel:(ManageCollectItemModel *)itemModel {
    if (itemModel) {
        _itemModel = itemModel;
        _contentLabel.text = itemModel.content;
        self.backgroundColor = RGB(0, 100, 10);
    }
}
-(void)editBtnClick {
    if (self.editBlock) {
        self.editBlock(self.itemModel);
    }
    
}
-(void)updateConstraints {
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
    }];
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-10);
        make.top.equalTo(self.contentView).offset(-15);
        make.size.mas_offset(CGSizeMake(30, 30));
    }];
    [super updateConstraints];
}
-(UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_editBtn setBackgroundImage:[UIImage imageNamed:@"del"] forState:UIControlStateNormal];
        [_editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editBtn;
}
-(UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = DEFAULT_RED_COLOR;
        _contentLabel.font = [UIFont systemFontOfSize:30];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _contentLabel;
}


@end

ManageCollectItemModel.h

#import <Foundation/Foundation.h>

@interface ManageCollectItemModel : NSObject
@property (nonatomic,assign) BOOL selected;
@property (nonatomic,strong) NSString *id;
@property (nonatomic,strong) NSString *content;
@end
上一篇下一篇

猜你喜欢

热点阅读