日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS 利用长按手势移动 Table View Cells

發(fā)布時間:2024/8/26 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 利用长按手势移动 Table View Cells 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文譯自:Cookbook: Moving Table View Cells with a Long Press Gesture

目錄:

  • 你需要什么?
  • 如何做?
  • 如何將其利用至UICollectionView上?
  • 何去何從?

本次的 cookbook-style 教程中介紹如何通過長按手勢來移動 table view中的cell,這種操作方式就像蘋果自家的天氣 App 一樣。

你可以直接把本文中的到嗎添加到你的工程中,或者將其添加到我為你創(chuàng)建好的 starter project 中,也可以下載本文的完整示例工程。

你需要什么?

  • UILongGestureRecognizer
  • UITableView (可以用 UICollectionView 替代之)
  • UITableViewController (可以用 UIViewController 或 UICollectionViewController 替代之)
  • 5 分鐘。

如何做?

首先給 table view 添加一個 UILongGestureRecognizer。可以在 table view controller 的 viewDidLoad 方法中添加。

1 2 3 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)]; [self.tableView addGestureRecognizer:longPress];

記者為 gesture recognizer 添加 action 方法。該方法首先應(yīng)該獲取到在 table view 中長按的位置,然后找出這個位置對應(yīng)的 cell 的 index。記住:這里獲取到的 index path 有可能為 nil(例如,如果用戶長按在 table view的section header上)。

1 2 3 4 5 6 7 8 9 10 - (IBAction)longPressGestureRecognized:(id)sender { UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender; UIGestureRecognizerState state = longPress.state; CGPoint location = [longPress locationInView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; // More coming soon... }

接著你需要處理UIGestureRecognizerStateBegan分支。如果獲取到一個有效的 index path(non-nil),就去獲取對應(yīng)的 UITableViewCell,并利用一個 helper 方法獲取這個 table view cell 的 snapshot view。然后將這個 snapshot view 添加到 table view 中,并將其 center 到對應(yīng)的 cell上。

為了更好的用戶體驗,以及更自然的效果,在這里我把原始 cell 的背景設(shè)置為黑色,并給 snapshot view 增加淡入效果,讓 snapshot view 比 原始 cell 稍微大一點,將它的Y坐標(biāo)偏移量與手勢的位置的Y軸對齊。這樣處理之后,cell 就像從 table view 中跳出,然后浮在上面,并捕捉到用戶的手指。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 static UIView *snapshot = nil; ///< A snapshot of the row user is moving. static NSIndexPath *sourceIndexPath = nil; ///< Initial index path, where gesture begins. switch (state) { case UIGestureRecognizerStateBegan: { if (indexPath) { sourceIndexPath = indexPath; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; // Take a snapshot of the selected row using helper method. snapshot = [self customSnapshotFromView:cell]; // Add the snapshot as subview, centered at cell's center... __block CGPoint center = cell.center; snapshot.center = center; snapshot.alpha = 0.0; [self.tableView addSubview:snapshot]; [UIView animateWithDuration:0.25 animations:^{ // Offset for gesture location. center.y = location.y; snapshot.center = center; snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05); snapshot.alpha = 0.98; // Black out. cell.backgroundColor = [UIColor blackColor]; } completion:nil]; } break; } // More coming soon... }

將下面的方法添加到 .m 文件的尾部。該方法會根據(jù)傳入的 view,返回一個對應(yīng)的 snapshot view。

1 2 3 4 5 6 7 8 9 10 11 - (UIView *)customSnapshotFromView:(UIView *)inputView { UIView *snapshot = [inputView snapshotViewAfterScreenUpdates:YES]; snapshot.layer.masksToBounds = NO; snapshot.layer.cornerRadius = 0.0; snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0); snapshot.layer.shadowRadius = 5.0; snapshot.layer.shadowOpacity = 0.4; return snapshot; }

當(dāng)手勢移動的時候,也就是UIGestureRecognizerStateChanged分支,此時需要移動 snapshot view(只需要設(shè)置它的 Y 軸偏移量即可)。如果手勢移動的距離對應(yīng)到另外一個 index path,就需要告訴 table view,讓其移動 rows。同時,你需要對 data source 進(jìn)行更新:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 case UIGestureRecognizerStateChanged: { CGPoint center = snapshot.center; center.y = location.y; snapshot.center = center; // Is destination valid and is it different from source? if (indexPath && ![indexPath isEqual:sourceIndexPath]) { // ... update data source. [self.objects exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row]; // ... move the rows. [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath]; // ... and update source so it is in sync with UI changes. sourceIndexPath = indexPath; } break; } // More coming soon...

最后,當(dāng)手勢結(jié)束或者取消時,table view 和 data source 都是最新的。你所需要做的事情就是將 snapshot view 從 table view 中移除,并把 cell 的背景色還原為白色。

為了提升用戶體驗,我們將 snapshot view 淡出,并讓其尺寸變小至與 cell 一樣。這樣看起來就像把 cell 放回原處一樣。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 default: { // Clean up. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndexPath]; [UIView animateWithDuration:0.25 animations:^{ snapshot.center = cell.center; snapshot.transform = CGAffineTransformIdentity; snapshot.alpha = 0.0; // Undo the black-out effect we did. cell.backgroundColor = [UIColor whiteColor]; } completion:^(BOOL finished) { [snapshot removeFromSuperview]; snapshot = nil; }]; sourceIndexPath = nil; break; }

就這樣,搞定了!編譯并運行程序,現(xiàn)在可以通過長按手勢對 tableview cells重新排序!

你可以在 GitHub 上下載到完整的示例工程。

如何將其利用至UICollectionView上?

假設(shè)你已經(jīng)有一個示例工程使用了 UICollectionView,那么你可以很簡單的就使用上本文之前介紹的代碼。所需要做的事情就是用 self.collectionView替換掉 self.tableView,并更新一下獲取和移動 UICollectionViewCell 的調(diào)用方法。

這里有個練習(xí),從 GitHub上 checkout 出 UICollectionView 的starter project,然后將 tap-和-hold 手勢添加進(jìn)去以對 cells 進(jìn)行重排。這里可以下載到已經(jīng)實現(xiàn)好了工程。

轉(zhuǎn)載于:https://www.cnblogs.com/jgCho/p/5262281.html

總結(jié)

以上是生活随笔為你收集整理的iOS 利用长按手势移动 Table View Cells的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 手机午夜视频 | 99这里都是精品 | 黄色网一级片 | 一本大道久久a久久精二百 琪琪色在线视频 | 精品在线播放视频 | 亚洲男女在线 | 亚洲男人天堂网 | 国产一区免费看 | 91黄版| 日产精品久久久一区二区 | 日韩精品无码一区二区三区久久久 | 极品少妇xxxx | 日韩欧美一区二区三区视频 | 日本αv| 亚洲综合视频在线观看 | 午夜精品电影 | 麻豆久久久久久久久久 | 亚洲经典视频 | 国产va亚洲va在线va | 538任你躁在线精品免费 | 久久99久久久 | 天天草夜夜操 | 欧美综合一区 | 毛片基地站 | 91成人免费在线 | 亚洲激情在线视频 | 曰本黄色片 | 91成人久久 | 在线观看涩涩视频 | 女女互慰揉小黄文 | 日韩av手机在线 | jizz中国少妇 | 一级片免费观看视频 | 麻豆精品久久久久久久99蜜桃 | 狠狠人妻久久久久久综合麻豆 | www.avcao| www.超碰97 | 亚洲国产视频一区二区三区 | 欧洲国产精品 | 毛片av在线观看 | 色综合天天综合网天天狠天天 | 日本久色 | 精品一区二区三区四区视频 | 国产精品自拍小视频 | 国毛片 | 一区二区三区亚洲 | 性xxxx另类xxⅹ | 日韩欧美国产一区二区 | 水多多在线| 精品一卡二卡 | 中文字幕日韩av | 国产亚洲欧美精品久久久www | 五月激情综合 | 国产精品熟女一区二区不卡 | 庆余年三 | jzz国产 | 女人脱下裤子让男人捅 | 最新黄色av | 草草屁屁影院 | 亚洲精品国产suv一区 | 午夜成人亚洲理伦片在线观看 | 亚洲永久免费网站 | 久久亚洲av无码精品色午夜麻豆 | 久久香蕉综合 | 亚洲欧美国产精品 | 亚洲免费观看在线 | 国产视频一区在线播放 | 国产精品九九视频 | 67194成人在线 | 免费在线a | 中年夫妇啪啪高潮 | 中出亚洲 | 欧洲女同同性吃奶 | 国产在线播放一区二区三区 | 精品一区二区三区免费看 | 亚洲系列在线观看 | 亚洲av无码电影在线播放 | 好吊一区二区三区视频 | 视频在线日韩 | 国产精品资源站 | 初尝黑人巨炮波多野结衣 | 贵族女沦为官妓h呻吟 | 超碰国产91 | 精品无码一区二区三区在线 | 日韩有码在线观看 | 三年中文免费观看大全动漫 | 欧美顶级少妇做爰 | 污污小说在线观看 | 日韩一二三四区 | 美女下部无遮挡 | 欧美混交群体交 | 一区二区三区四区人妻 | 亚洲视频国产 | 国产成人区 | 欧美日性视频 | 欧美骚少妇| 免费精品视频在线观看 | 亚洲精品在线影院 | 国产精品中文久久久久久 |