ios 自定義菜單顯示內(nèi)容(可拖拽排序collectionView)

非常感謝大家利用自己寶貴的時間來閱讀我的文章 , ?最近項目有個首頁菜單定制的需求,類似于支付寶的首頁模塊定制危队, 如果有這種需求的小伙伴不妨看一下,希望能幫到你 , 當然, 有任何不妥的地方 歡迎指正。喜歡的可以關(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ū)問我

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市撑帖,隨后出現(xiàn)的幾起案子蓉坎,更是在濱河造成了極大的恐慌,老刑警劉巖胡嘿,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蛉艾,死亡現(xiàn)場離奇詭異,居然都是意外死亡衷敌,警方通過查閱死者的電腦和手機勿侯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缴罗,“玉大人助琐,你說我怎么就攤上這事÷髋溃” “怎么了弓柱?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長侧但。 經(jīng)常有香客問我矢空,道長,這世上最難降的妖魔是什么禀横? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任屁药,我火速辦了婚禮,結(jié)果婚禮上柏锄,老公的妹妹穿的比我還像新娘酿箭。我一直安慰自己,他們只是感情好趾娃,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布缭嫡。 她就那樣靜靜地躺著,像睡著了一般抬闷。 火紅的嫁衣襯著肌膚如雪妇蛀。 梳的紋絲不亂的頭發(fā)上耕突,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音评架,去河邊找鬼眷茁。 笑死,一個胖子當著我的面吹牛纵诞,可吹牛的內(nèi)容都是我干的上祈。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼浙芙,長吁一口氣:“原來是場噩夢啊……” “哼登刺!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起茁裙,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤塘砸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后晤锥,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡廊宪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年矾瘾,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片箭启。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡壕翩,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出傅寡,到底是詐尸還是另有隱情放妈,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布荐操,位于F島的核電站芜抒,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏托启。R本人自食惡果不足惜宅倒,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望屯耸。 院中可真熱鬧拐迁,春花似錦、人聲如沸疗绣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽多矮。三九已至缓淹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背割卖。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工前酿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鹏溯。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓罢维,卻偏偏與公主長得像,于是被迫代替她去往敵國和親丙挽。 傳聞我的和親對象是個殘疾皇子肺孵,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容