非常感謝大家利用自己寶貴的時間來閱讀我的文章 , ?最近項目有個首頁菜單定制的需求,類似于支付寶的首頁模塊定制危队, 如果有這種需求的小伙伴不妨看一下,希望能幫到你 , 當然, 有任何不妥的地方 歡迎指正。喜歡的可以關(guān)注下我的簡書阐滩、我的博客
首先看一下效果展示吧(請忽略這個不忍直視的GIF??)
需要的話可以去gitHub上下載--ZQVariableMenu
用法:把下載的demo里的ZQVariableMenu文件夾拷入項目文件中坪稽,在控制器中引用ZQVariableMenuControl.h,然后在點擊響應方法里實現(xiàn)下面代碼
NSArray *array1 = @[];//需要展示的title數(shù)組
NSArray *array2 = @[];//未展示的title數(shù)組
[[ZQVariableMenuControl shareControl] showChannelViewWithInUseTitles:array1 unUseTitles:array2 fixedNum:1 finish:^(NSArray *inUseTitles, NSArray *unUseTitles) {
NSLog(@"inUseTitles = %@",inUseTitles);
NSLog(@"unUseTitles = %@",unUseTitles);
}];
其中array1 為需要展示的title數(shù)組扛伍,array2為未展示的title數(shù)組筷畦,fixedNum需要固定的title數(shù)量,這里我只是實現(xiàn)讓前面fixedNum個不能移動,可以根據(jù)自己的需要進行修改鳖宾,具體代碼位置在ZQVariableMenuView的數(shù)據(jù)源方法里,
實現(xiàn)思路:
1吼砂、為collectionView添加長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMethod:)];
longPress.minimumPressDuration = 0.3f;
[_collectionView addGestureRecognizer:longPress];
2、監(jiān)聽手勢變化
#pragma mark LongPressMethod
-(void)longPressMethod:(UILongPressGestureRecognizer*)gesture{
CGPoint point = [gesture locationInView:_collectionView];
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
[self dragBegin:point];
break;
case UIGestureRecognizerStateChanged:
[self dragChanged:point];
break;
case UIGestureRecognizerStateEnded:
[self dragEnd];
break;
default:
break;
}
}
2.1在拖拽開始的時候找到被拖拽的item
-(void)dragBegin:(CGPoint)point{
_dragingIndexPath = [self getDragingIndexPathWithPoint:point];
if (!_dragingIndexPath) {return;}
[_collectionView bringSubviewToFront:_dragingItem];
ZQVariableMenuCell *item = (ZQVariableMenuCell*)[_collectionView cellForItemAtIndexPath:_dragingIndexPath];
item.isMoving = true;
item.hidden = YES;
//更新被拖拽的item
_dragingItem.hidden = false;
_dragingItem.frame = item.frame;
_dragingItem.title = item.title;
_dragingItem.imageName = item.imageName;
[_dragingItem setTransform:CGAffineTransformMakeScale(1.1, 1.1)];
}
2.2拖拽過程中交換位置及更新數(shù)據(jù)源
-(void)dragChanged:(CGPoint)point{
if (!_dragingIndexPath) {return;}
_dragingItem.center = point;
_targetIndexPath = [self getTargetIndexPathWithPoint:point];
//交換位置 如果沒有找到_targetIndexPath則不交換位置
if (_dragingIndexPath && _targetIndexPath) {
//更新數(shù)據(jù)源
[self rearrangeInUseTitles];
//更新item位置
[_collectionView moveItemAtIndexPath:_dragingIndexPath toIndexPath:_targetIndexPath];
_dragingIndexPath = _targetIndexPath;
}
}
2.3拖拽結(jié)束后更新itemframe及狀態(tài)
//拖拽結(jié)束
-(void)dragEnd{
if (!_dragingIndexPath) {return;}
CGRect endFrame = [_collectionView cellForItemAtIndexPath:_dragingIndexPath].frame;
[_dragingItem setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
[UIView animateWithDuration:0.3 animations:^{
_dragingItem.frame = endFrame;
}completion:^(BOOL finished) {
_dragingItem.hidden = true;
ZQVariableMenuCell *item = (ZQVariableMenuCell*)[_collectionView cellForItemAtIndexPath:_dragingIndexPath];
item.isMoving = false;
item.hidden = NO;
}];
}
獲取當前拖動item的IndexPath方法這里我設置的只剩一個的時候和待選菜單不可排序,需要固定位置的前幾個item不需要排序.
-(NSIndexPath*)getDragingIndexPathWithPoint:(CGPoint)point{
NSIndexPath* dragIndexPath = nil;
//最后剩一個不可以排序
if ([_collectionView numberOfItemsInSection:0] == 1) {return dragIndexPath;}
for (NSIndexPath *indexPath in _collectionView.indexPathsForVisibleItems) {
//下半部分不需要排序
if (indexPath.section > 0) {continue;}
//需要固定位置的前幾個item不需要排序
if (indexPath.row<self.fixedNum) {continue;}
//在上半部分中找出相對應的Item
if (CGRectContainsPoint([_collectionView cellForItemAtIndexPath:indexPath].frame, point)) {
if (indexPath.row != 0) {
dragIndexPath = indexPath;
}
break;
}
}
return dragIndexPath;
}
獲取目標IndexPath的方法
-(NSIndexPath*)getTargetIndexPathWithPoint:(CGPoint)point{
NSIndexPath *targetIndexPath = nil;
for (NSIndexPath *indexPath in _collectionView.indexPathsForVisibleItems) {
//如果是自己不需要排序
if ([indexPath isEqual:_dragingIndexPath]) {continue;}
//第二組不需要排序
if (indexPath.section > 0) {continue;}
//需要固定位置的前幾個item不需要排序
if (indexPath.row<self.fixedNum) { continue; }
//在第一組中找出將被替換位置的Item
if (CGRectContainsPoint([_collectionView cellForItemAtIndexPath:indexPath].frame, point)) {
if (indexPath.row != 0) {
targetIndexPath = indexPath;
}
}
}
return targetIndexPath;
}
在demo里我做了個數(shù)據(jù)存儲鼎文,記住用戶當前的選擇
1渔肩、路徑
#define ShowMenusPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject] stringByAppendingPathComponent:@"Menus.txt"]
2、取出存儲的數(shù)據(jù)
_currentMenus = [NSArray arrayWithContentsOfFile:ShowMenusPath];
3拇惋、存儲改變后的數(shù)據(jù)
[[ZQVariableMenuControl shareControl] showChannelViewWithInUseTitles:_currentMenus unUseTitles:UnShowMenus fixedNum:1 finish:^(NSArray *inUseTitles, NSArray *unUseTitles) {
[inUseTitles writeToFile:ShowMenusPath atomically:YES];
[self reloadCollectionView];
}];
可以根據(jù)自己實際需要去自定義item周偎,有什么疑問可以在評論區(qū)問我