新聞中心
近日因?yàn)橄到y(tǒng)升級(jí)導(dǎo)致xcode6.系列版本出現(xiàn)bug,于是開始使用xcode7。在使用之余突然想到collectionView在iOS9中發(fā)布了一個(gè)可以移動(dòng)cell的新特性,就嘗試著將其實(shí)現(xiàn),無奈api文檔接口無法查看,只有一些列的api放在那里。于是上網(wǎng)查找,發(fā)現(xiàn)國內(nèi)沒有搜索到此類文章,于是FQ繼續(xù)找,最終找到的竟然都是swift版本,于是將其轉(zhuǎn)換為oc版本以幫助國內(nèi)需要的朋友學(xué)習(xí)使用。下面是具體用法:

專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)南皮免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
1.創(chuàng)建collectionView并設(shè)置代理
- (UICollectionView *)collectionView{
if (_collectionView == nil) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(50, 50);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 10;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
_collectionView.backgroundColor = [UIColor cyanColor];
_collectionView.dataSource = self;
//此處給其增加長按手勢(shì),用此手勢(shì)觸發(fā)cell移動(dòng)效果
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
[_collectionView addGestureRecognizer:longGesture];
}
return _collectionView;
}2.設(shè)置其資源
_dataSource = [NSMutableArray array];
for (int i = 1; i <= 50; i++) {
NSString *imageName = [NSString stringWithFormat:@"%d",i];
[_dataSource addObject:imageName];
}3.監(jiān)聽手勢(shì),并設(shè)置其允許移動(dòng)cell和交換資源
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
//判斷手勢(shì)狀態(tài)
switch (longGesture.state) {
case UIGestureRecognizerStateBegan:{
//判斷手勢(shì)落點(diǎn)位置是否在路徑上
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
if (indexPath == nil) {
break;
}
//在路徑上則開始移動(dòng)該路徑上的cell
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
case UIGestureRecognizerStateChanged:
//移動(dòng)過程當(dāng)中隨時(shí)更新cell位置
[self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
break;
case UIGestureRecognizerStateEnded:
//移動(dòng)結(jié)束后關(guān)閉cell移動(dòng)
[self.collectionView endInteractiveMovement];
break;
default:
[self.collectionView cancelInteractiveMovement];
break;
}
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
//返回YES允許其item移動(dòng)
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
//取出源item數(shù)據(jù)
id objc = [_dataSource objectAtIndex:sourceIndexPath.item];
//從資源數(shù)組中移除該數(shù)據(jù)
[_dataSource removeObject:objc];
//將數(shù)據(jù)插入到資源數(shù)組中的目標(biāo)位置上
[_dataSource insertObject:objc atIndex:destinationIndexPath.item];
} 通過以上設(shè)置便可以成功移動(dòng)cell了,下面奉上效果圖
至此collectionView的新特性使用方法展示完成,如其中有何錯(cuò)誤之處望之處,謝謝!
我認(rèn)為你會(huì)是我的全部,我為此拼搏奮斗,希望***不只是我認(rèn)為!
新聞名稱:iOS9collectionView新特性
URL分享:http://fisionsoft.com.cn/article/djdoiei.html


咨詢
建站咨詢
