iOS 新闻类型拖拽排序

最终效果图:


#import "XinWenViewController.h"

@interface XinWenViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{

UICollectionView *_collectionView;

}

#pragma -mark创建新闻类型视图

- (void)createXinWenLeiXingShiTu{

//创建流布局设置类

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

//设置为垂直布局方向

layout.scrollDirection = UICollectionViewScrollDirectionVertical;

//设置每个item的大小

layout.itemSize = CGSizeMake(60, 30);

//

[layout setSectionInset:UIEdgeInsetsMake(20, 20, 20, 20)];

//

_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64) collectionViewLayout:layout];

_collectionView.backgroundColor = [UIColor whiteColor];

_collectionView.dataSource = self;

_collectionView.delegate = self;

// 添加长按手势

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];

[_collectionView addGestureRecognizer:longPress];

//注册单元格

[_collectionView registerClass:[XInWenNaviCell class] forCellWithReuseIdentifier:@"cell"];

//注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView

[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cell"];

//创建新闻类型视图

_xinWenLeiXing = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64)];

[_xinWenLeiXing addSubview:_collectionView];

[self.view addSubview:_xinWenLeiXing];

_xinWenLeiXing.backgroundColor = [UIColor whiteColor];

}

#pragma -mark设置单元格个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

if (section == 0) {

return [_wholeAry[section] count];

}

else{

return [_wholeAry[section] count];

}

}

#pragma -mark设置单元格

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

XInWenNaviCell *cell = (XInWenNaviCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

NSArray *ary = _wholeAry[indexPath.section];

cell.naviLbl.text = ary[indexPath.row];

return cell;

}

#pragma -mark设置分组

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

return 2;

}

#pragma -mark设置每个item垂直间距

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

{

return 30;

}

#pragma -mark设置header的size

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

return CGSizeMake(kWidth, 40);

}

#pragma -mark设置头视图

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

{

//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cell" forIndexPath:indexPath];

if (indexPath.section == 0) {

UILabel *woDe = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 40)];

woDe.text = @"我的频道";

woDe.font = [UIFont systemFontOfSize:20];

[headerView addSubview:woDe];

//

UILabel *paiXu = [[UILabel alloc]initWithFrame:CGRectMake(100, 5, 100, 40)];

paiXu.text = @"拖拽可以排序";

paiXu.font = [UIFont systemFontOfSize:14];

[paiXu setTextColor:[UIColor colorWithRed:220.0/255 green:220.0/255 blue:220.0/255 alpha:1]];

[headerView addSubview:paiXu];

//

UIView *wanChengView = [[UIView alloc]initWithFrame:CGRectMake(kWidth-80, 10, 60, 30)];

wanChengView.layer.cornerRadius = 15;

wanChengView.layer.borderWidth = 1;

wanChengView.layer.borderColor = [[UIColor colorWithRed:255.0/255 green:99.0/255 blue:74.0/255 alpha:1] CGColor];

[headerView addSubview:wanChengView];

//

UIButton *wanChengBtn = [UIButton buttonWithType:UIButtonTypeCustom];

wanChengBtn.frame = CGRectMake(kWidth-80, 10, 60, 30);

[wanChengBtn setTitleColor:[UIColor colorWithRed:255.0/255 green:99.0/255 blue:74.0/255 alpha:1] forState:UIControlStateNormal];

[wanChengBtn setTitle:@"完成" forState:UIControlStateNormal];

[wanChengBtn addTarget:self action:@selector(wanCheng:) forControlEvents:UIControlEventTouchUpInside];

[headerView addSubview:wanChengBtn];

}

else{

UILabel *tuiJian = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 40)];

tuiJian.text = @"频道推荐";

tuiJian.font = [UIFont systemFontOfSize:20];

[headerView addSubview:tuiJian];

UILabel *tianJia = [[UILabel alloc]initWithFrame:CGRectMake(100, 5, 100, 40)];

tianJia.text = @"拖拽可以排序";

tianJia.font = [UIFont systemFontOfSize:14];

[tianJia setTextColor:[UIColor colorWithRed:220.0/255 green:220.0/255 blue:220.0/255 alpha:1]];

[headerView addSubview:tianJia];

}

return headerView;

}

#pragma -mark长按调用的方法

- (void)handlelongGesture:(UILongPressGestureRecognizer *)recognizer{

//判断系统版本

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {

// [self action:longPress];

} else {

[self iOS9_Action:recognizer];

}

}

#pragma -mark允许row移动

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath

{

// 返回YES允许row移动

return YES;

}

#pragma -mark移动完成时

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

//当前组

NSString *sourceKey = _wholeAry[sourceIndexPath.section];

//目标组

NSString *destinationKey = _wholeAry[destinationIndexPath.section];

if (sourceKey == destinationKey) {

//----------------------同一组移动时----------------------

//取出移动row数据

id yiDong = _wholeAry[sourceIndexPath.section][sourceIndexPath.row];

//从数据源中移除该数据

[_wholeAry[sourceIndexPath.section] removeObject:yiDong];

//将数据插入到数据源中的目标位置

[_wholeAry[sourceIndexPath.section] insertObject:yiDong atIndex:destinationIndexPath.row];

}

else{

//------------------------不同组移动时---------------------

//取出移动row数据

id yiDong = _wholeAry[sourceIndexPath.section][sourceIndexPath.row];

//从数据源中移除该数据

[_wholeAry[sourceIndexPath.section] removeObject:yiDong];

//将数据插入到数据源中的目标位置

[_wholeAry[destinationIndexPath.section] insertObject:yiDong atIndex:destinationIndexPath.row];

}

}

#pragma -mark长按手势调用

- (void)iOS9_Action:(UILongPressGestureRecognizer *)longPress

{

switch (longPress.state) {

case UIGestureRecognizerStateBegan:

{ //手势开始

//判断手势落点位置是否在row上

NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:[longPress locationInView:_collectionView]];

if (indexPath == nil) {

break;

}

UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath];

[self.view bringSubviewToFront:cell];

//iOS9方法 移动cell

[_collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];

}

break;

case UIGestureRecognizerStateChanged:

{ // 手势改变

// iOS9方法 移动过程中随时更新cell位置

[_collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_collectionView]];

}

break;

case UIGestureRecognizerStateEnded:

{ // 手势结束

// iOS9方法 移动结束后关闭cell移动

[_collectionView endInteractiveMovement];

}

break;

default: //手势其他状态

[_collectionView cancelInteractiveMovement];

break;

}

}

#pragma -mark完成按钮调用

- (void)wanCheng:(UIButton *)sender{

// 删除原滚动视图

[_naviScrollView removeFromSuperview];

//创建新滚动视图  更新新闻类型

[self createNaviShiTU:_naviAry.count];

//删除新闻类型视图

[_xinWenLeiXing removeFromSuperview];

//显示标签页控制器

self.tabBarController.tabBar.hidden=NO;

}


下面是单元格

#import@interface XInWenNaviCell : UICollectionViewCell

@property (nonatomic,strong) UILabel *naviLbl;//设置文字

@property (nonatomic,strong) UIView *naviView;//设置底部的视图

@end


#import "XInWenNaviCell.h"

@implementation XInWenNaviCell

- (instancetype)initWithFrame:(CGRect)frame{

self = [super initWithFrame:frame];

if (self) {

//设置视图

_naviView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];

_naviView.layer.cornerRadius = 10;

_naviView.layer.borderWidth = 1;

_naviView.backgroundColor = [UIColor colorWithRed:245.0/255 green:245.0/255 blue:245.0/255 alpha:1];

[self.contentView addSubview:_naviView];

//设置文字

_naviLbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 0, 40, 40)];

[self.contentView addSubview:_naviLbl];

}

return self;

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容