iOS标签管理
来源:互联网 发布:天敏智能网络机顶盒 编辑:程序博客网 时间:2024/06/15 01:00
这个是使用UICollectionView 在iOS9之后新加入的一个方法可以很便捷的实现这个功能。
<span style="font-family: Arial, Helvetica, sans-serif;">定义一个全局的cell以及两个数据源数组:</span>
<pre name="code" class="objc">@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{ UICollectionViewCell * cell;}@property(nonatomic,retain)UICollectionView *collectionView;@property(nonatomic,strong)NSMutableArray *array1;@property(nonatomic,strong)NSMutableArray *array2;@end
接下来ViewDidload里面
_array1 = [NSMutableArray array]; _array2 = [NSMutableArray array]; UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 30)]; lab.text = @"推荐频道"; lab.backgroundColor = [UIColor yellowColor]; [self.view addSubview:lab]; for (int i = 0; i< 20; i++) { UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; btn1.frame = CGRectMake(0, 0, 50, 50); [btn1 setTitle:[NSString stringWithFormat:@"A%d",i] forState:UIControlStateNormal]; btn1.userInteractionEnabled = NO; btn1.backgroundColor = [UIColor redColor]; [_array1 addObject:btn1]; } for (int i = 15; i <30; i++) { UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; btn2.frame = CGRectMake(0, 0, 50, 50); [btn2 setTitle:[NSString stringWithFormat:@"B%d",i] forState:UIControlStateNormal]; btn2.userInteractionEnabled = NO; [_array2 addObject:btn2]; } //这个用来定义水平滚动还是垂直滚动 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; [layout setScrollDirection:UICollectionViewScrollDirectionVertical]; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 94, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout]; //layout.minimumLineSpacing = 10; //layout.minimumInteritemSpacing = 10; //注册Cell,必须要有 [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; _collectionView.backgroundColor = [UIColor grayColor]; _collectionView.dataSource = self; _collectionView.delegate = self; //此处给其增加长按手势,用此手势触发cell移动效果 UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)]; [_collectionView addGestureRecognizer:longGesture]; [self.view addSubview:_collectionView]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 210, self.view.bounds.size.width, 30)]; label.backgroundColor = [UIColor yellowColor]; label.text = @"点击添加"; label.textAlignment = NSTextAlignmentCenter; //[self.view addSubview:label];
</pre> 然后就是 <span style="background-color: rgb(240, 240, 240);">CollectionView 的代理方法</span><p></p><p></p><pre name="code" class="objc">//Section的个数-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 2;}//Cell的个数-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if (section == 0) { return _array1.count; }else return _array2.count;}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ //这个cell的标识必须使用上面注册的 static NSString * CellIdentifier = @"Cell"; cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; if (!cell) { cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; } cell.backgroundColor = [UIColor blueColor]; if (indexPath.section == 0) { [cell addSubview:_array1[indexPath.item]]; return cell; }else [cell addSubview:_array2[indexPath.item]]; return cell; }
//定义每个UICollectionView 的 margin (边缘距离)-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ //上左下右~ return UIEdgeInsetsMake(5, 5, 99, 5);}//设置每个item水平间距- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 10;}//设置每个item垂直间距- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return 15;}//允许item移动- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{ return YES;}
//是否可以被选择-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{ return YES;}
//UICollectionView被选中时调用的方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ [cell removeFromSuperview]; if (indexPath.section == 0) { UIButton *bb = _array1[indexPath.item]; [_array1 removeObjectAtIndex:indexPath.item]; [_array2 insertObject:bb atIndex:_array2.count]; [_collectionView reloadData]; }else { UIButton *cc = _array2[indexPath.item]; [_array2 removeObjectAtIndex:indexPath.item]; [_array1 insertObject:cc atIndex:_array1.count]; [_collectionView reloadData]; } }
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture { //判断手势状态 switch (longGesture.state) { case UIGestureRecognizerStateBegan:{ //判断手势落点位置是否在路径上 NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]]; if (indexPath == nil) { break; } //在路径上则开始移动该路径上的cell [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath]; } break; case UIGestureRecognizerStateChanged: //移动过程当中随时更新cell位置 [self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]]; break; case UIGestureRecognizerStateEnded: //移动结束后关闭cell移动 [self.collectionView endInteractiveMovement]; break; default: [self.collectionView cancelInteractiveMovement]; break; }}
//移动结束,替换数据-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath { //两个section if (sourceIndexPath.section == 0 && destinationIndexPath.section == 1) { UIButton *bb = _array1[sourceIndexPath.item]; [_array1 removeObjectAtIndex:sourceIndexPath.item]; [_array2 insertObject:bb atIndex:destinationIndexPath.item]; }else if (sourceIndexPath.section == 1 && destinationIndexPath.section == 0) { UIButton *cc = _array2[sourceIndexPath.item]; [_array2 removeObjectAtIndex:sourceIndexPath.item]; [_array1 insertObject:cc atIndex:destinationIndexPath.item]; } //同一个section else if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) { UIButton *aa = _array1[sourceIndexPath.item]; [_array1 removeObjectAtIndex:sourceIndexPath.item]; [_array1 insertObject:aa atIndex:destinationIndexPath.item]; } else { UIButton *dd = _array2[sourceIndexPath.item]; [_array2 removeObjectAtIndex:sourceIndexPath.item]; [_array2 insertObject:dd atIndex:destinationIndexPath.item]; } }
0 0
- iOS标签管理
- 标签管理
- 标签管理
- 标签管理
- 标签管理
- 源码管理十六:标签管理
- 如何管理标签?
- Gmail 新增标签管理
- 22.标签管理
- github之标签管理
- git 标签管理(六)
- Git标签管理
- 22. 标签管理
- Git 标签管理
- git标签管理
- 浏览器标签管理插件
- git 标签管理
- Git标签管理
- Python:渗透测试开源项目【源码值得精读】
- CSS3 Transitions, Transforms和Animation使用简介
- css3loading状态2
- JDBC 事务隔离级别
- 使用sql连接sqlserver进行增删查改基本操作类
- iOS标签管理
- XStream序列化对象,java.util.Map自定义Converter
- ios10测试版的真机在xcode8(beta)下编译报错 library not found for -lcrt1.3.1.o的解决办法
- java虚拟机采用抢占式调度模型
- Android 冷知识(1)开机广播及一些特殊广播
- POJ - 3619-Speed Reading
- PHP遍历文件夹目录
- 项目中---关于AlertDialog的显示
- CentOS 7 扩展根目录空间操作步骤