iOS 新聞類型拖拽排序

最終效果圖:


#import "XinWenViewController.h"

@interface XinWenViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{

UICollectionView *_collectionView;

}

#pragma -mark創(chuàng)建新聞類型視圖

- (void)createXinWenLeiXingShiTu{

//創(chuàng)建流布局設置類

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"];

//創(chuàng)建新聞類型視圖

_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{

//判斷系統(tǒng)版本

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: //手勢其他狀態(tài)

[_collectionView cancelInteractiveMovement];

break;

}

}

#pragma -mark完成按鈕調用

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

// 刪除原滾動視圖

[_naviScrollView removeFromSuperview];

//創(chuàng)建新滾動視圖? 更新新聞類型

[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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容