iOS Drag And Drop使用

文章結構

  • drag and drop簡介
  • tableView之Drag and Drop
  • UITextView/UITextField之Drag and Drop
  • 自定義Drag and Drop View

drag and drop簡介

翻譯過來就是拖拽,可以將數(shù)據(jù)從屏幕一個地方拖拽到另外一個地方,可以在同一個app內實現(xiàn)拖拽,也可以跨app實現(xiàn)數(shù)據(jù)拖拽。

所有的拖拽功能在iPad上都可用,在iPhone上拖拽只能在同一個app內。

tableView之Drag and Drop

打開tableView的拖拽功能,代碼如下:

self.tableView.dragInteractionEnabled = YES;//打開拖拽功能
//實現(xiàn)拖拽的代理
self.tableView.dragDelegate = self;
self.tableView.dropDelegate = self;

drag必須實現(xiàn)的代理方法:

- (NSArray<UIDragItem *> *)tableView:(UITableView *)tableView itemsForBeginningDragSession:(id<UIDragSession>)session atIndexPath:(NSIndexPath *)indexPath;

該代理實現(xiàn)拖拽數(shù)據(jù)的構建。

drop必需實現(xiàn)的代理

- (void)tableView:(UITableView *)tableView performDropWithCoordinator:(id<UITableViewDropCoordinator>)coordinator;

該代理接收拖拽的數(shù)據(jù)。

demo里實現(xiàn)了類似支付寶重設扣款順序的交互效果。
各個代理方法在demo里有所解釋,請下載demo查看。
UICollectionView和UITableVIew類似。

UITextView/UITextField之Drag and Drop

可以直接進行拖拽,不需要進行任何代碼處理,系統(tǒng)默認處理的UI交互已經(jīng)做得很到位了。

自定義Drag and Drop View

只要是UIView類或者其子類都可以實現(xiàn)拖拽功能。
在UIView中有一個類別

@interface UIView (Interactions)

- (void)addInteraction:(id<UIInteraction>)interaction API_AVAILABLE(ios(11.0), watchos(5.0), tvos(13.0));
- (void)removeInteraction:(id<UIInteraction>)interaction API_AVAILABLE(ios(11.0), watchos(5.0), tvos(13.0));

@property (nonatomic, copy) NSArray<id<UIInteraction>> *interactions API_AVAILABLE(ios(11.0), watchos(5.0), tvos(13.0));

@end

該類別就是給UIView或其子類添加拖拽功能所需要的。
添加拖拽功能步驟如下:

  1. 給view添加UIDragInteraction、UIDropInteraction,UIDragInteraction允許view構建數(shù)據(jù)用于拖拽,UIDropInteraction允許view接收來自于拖拽的數(shù)據(jù)。demo里DragView只能構建數(shù)據(jù)用于拖拽,不能接收來自于拖拽的數(shù)據(jù),DropView反之。
  2. 實現(xiàn)拖拽響應的代理方法。

UIDragInteraction重要的代理方法如下:

- (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction itemsForBeginningSession:(id<UIDragSession>)session;

必須實現(xiàn)的代理方法,用于構建拖拽數(shù)據(jù)。

- (BOOL)dragInteraction:(UIDragInteraction *)interaction sessionAllowsMoveOperation:(id<UIDragSession>)session;

數(shù)據(jù)是否允許移動,這一般發(fā)生在同一app內,如果是跨app的拖拽則都是copy操作。

注意,該代理返回的值,直接影響UIDropInteraction代理方法- (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session; 里是否允許對拖拽數(shù)據(jù)進行移動處理。

UIDropInteraction重要的代理方法如下:

- (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session;

能否處理該拖拽,一般在這里進行數(shù)據(jù)類型檢查。

- (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session;

對拖拽數(shù)據(jù)的處理方法,取消、禁止、拷貝、移動。如果是跨app間的處理必須都是copy。

- (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session;

接收拖拽數(shù)據(jù)。

demo里拖拽的數(shù)據(jù)都是NSString類型。能實現(xiàn)拖拽的數(shù)據(jù)類型必須遵循NSItemProviderWritingNSItemProviderReading協(xié)議。系統(tǒng)已經(jīng)默認實現(xiàn)了這兩個協(xié)議的類有 NSString, NSAttributedString, NSURL, UIColor, UIImage

自定義控件的拖拽功能,目前在iphone上實現(xiàn)不了,iPad上沒問題,沒見相關官方文檔說明。
Demo傳送

參考文章:
Drag and Drop

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容