章节
Drag & Drop除了基础APIs外,还为tableView,collectionView、textView提供了高级APIs,所以本系列分为以下几个部分:
- 基础视图
- UITableView
- UICollectionView
- UISpringLoadedInteraction(本篇)
什么是Spring Load?
弹性加载?,用于Drag Drop时的动态UI导航。这样翻译貌似也有点别扭,云里雾里。。。还是用个动图解释什么是Spring load
![](https://img.haomeiwen.com/i1751666/07c75bd637b76449.gif)
图里Drag到tableView Cell上,cell闪烁,push到下层UI,这个过程就叫做SpringLoad,就像你在mac上拖动文件到某个文件夹上,会导航进入这个文件夹一样。
如何使用Spring Load?
- 系统默认支持
iOS11 默认支持的标准UIKit
- MKUserTrackingBarButtonItem
- PKAddPassButton
- PKPaymentButton
- UIAlertController
- UIBarButtonItem
- UIButton
- UICollectionView
- UISegmentedControl
- UITabBar
- UITabBarItem
- UITableView
对于默认支持的控件,实现非常简单,打开对应视图的SpringLoad开关即可,例如上图tableView:
self.tableView.springLoaded = YES;
cell原有跳转逻辑不变。
当然,如果需要自定义SpringLoad触发事件(即视图闪烁后),参考2.
- 添加自定义Spring load
这里使用UIImageView来实现上述UI导航效果,代码也很简单:
self.springLoadImageView.frame = CGRectMake(0, CGRectGetHeight(self.view.frame)-300, CGRectGetWidth(self.view.frame), 300);
[self.view addSubview:self.springLoadImageView];
self.springLoadImageView.userInteractionEnabled = YES;
__weak __typeof(self) weakSelf = self;
UISpringLoadedInteraction *springLoadInteraction = [[UISpringLoadedInteraction alloc] initWithActivationHandler:^(UISpringLoadedInteraction * _Nonnull interaction, id<UISpringLoadedInteractionContext> _Nonnull context) {
Class cls = NSClassFromString([weakSelf.demos objectAtIndex:1][@"class"]);
UIViewController *vc = (UIViewController*)[[cls alloc] init];
[weakSelf.navigationController pushViewController:vc animated:YES];
}];
[self.springLoadImageView addInteraction:springLoadInteraction];
![](https://img.haomeiwen.com/i1751666/1665c5757956b9a7.gif)
参考资料
文档
视频
- Introducing Drag and Drop
- Mastering Drag and Drop
- Drag and Drop with Collection and Table View
- Data Delivery with Drag and Drop
代码
Tips
文章难免会有错误、理解错位的地方,请不吝指教。🙏
网友评论