• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >swift > Swift下使用UICollectionView 实现长按拖拽功能

Swift下使用UICollectionView 实现长按拖拽功能

作者:coderLiu 字体:[增加 减小] 来源:互联网 时间:2017-05-28

coderLiu 通过本文主要向大家介绍了uicollectionview,ios uicollectionview,excel拖拽功能,鼠标拖拽功能失灵,鼠标拖拽功能等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

导读

简单用Swift写了一个collectionview的拖拽点击排序效果;

拖拽排序是新闻类的App可以说是必有的交互设计,如今日头条,网易新闻等。

GitHub地址:https://github.com/wangliujiayou/Swift-dragLabel 欢迎Star.

效果

主要代码

手势长按移动

1.给CollectionViewCell添加一个长按手势.

private lazy var collectionView: UICollectionView = {
  let clv = UICollectionView(frame: self.view.frame, collectionViewLayout: ChannelViewLayout())
  clv.backgroundColor = UIColor.white
  clv.delegate = self
  clv.dataSource = self
  clv.register(ChannelViewCell.self, forCellWithReuseIdentifier: ChannelViewCellIdentifier)
  clv.register(ChannelHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: ChannelViewHeaderIdentifier)
  let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressGesture(_:)))
  clv.addGestureRecognizer(longPress)
  return clv
 }()
</div>

2.开始长按时对cell进行截图或拷贝一个cell,并隐藏cell.

 //MARK: - 长按开始
 private func dragBegan(point: CGPoint) {
  indexPath = collectionView.indexPathForItem(at: point)
  if indexPath == nil || (indexPath?.section)! > 0 || indexPath?.item == 0
  {return}
  let item = collectionView.cellForItem(at: indexPath!) as? ChannelViewCell
  item?.isHidden = true
  dragingItem.isHidden = false
  dragingItem.frame = (item?.frame)!
  dragingItem.text = item!.text
  //放大效果(此处可以根据需求随意修改)
  dragingItem.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
 }
</div>

3.在手势移动的时候,找到目标是的indexPatch,再调用系统的api交换这个cell和隐藏cell的位置,并且更新数据.

 //MARK: - 移动过程
 private func drageChanged(point: CGPoint) {
  if indexPath == nil || (indexPath?.section)! > 0 || indexPath?.item == 0 {return}
  dragingItem.center = point
  targetIndexPath = collectionView.indexPathForItem(at: point)
  if targetIndexPath == nil || (targetIndexPath?.section)! > 0 || indexPath == targetIndexPath || targetIndexPath?.item == 0 {return}
  // 更新数据
  let obj = selectedArr[indexPath!.item]
  selectedArr.remove(at: indexPath!.row)
  selectedArr.insert(obj, at: targetIndexPath!.item)
  //交换位置
  collectionView.moveItem(at: indexPath!, to: targetIndexPath!)
  //进行记录
  indexPath = targetIndexPath
 }
</div>

4.手势停止或取消时,移除view,显示隐藏cell. (这里手势取消也要掉用此方法)

//MARK: - 长按结束或取消
 private func drageEnded(point: CGPoint) {
  if indexPath == nil || (indexPath?.section)! > 0 || indexPath?.item == 0 {return}
  let endCell = collectionView.cellForItem(at: indexPath!)
  UIView.animate(withDuration: 0.25, animations: {
   self.dragingItem.transform = CGAffineTransform.identity
   self.dragingItem.center = (endCell?.center)!
  }, completion: {
   (finish) -> () in
   endCell?.isHidden = false
   self.dragingItem.isHidden = true
   self.indexPath = nil
  })
 }
</div>

点击移动

collectionView的点击方法,我这里分为两段,第一段为点击处理事件,第二段为点击添加添加标签(编辑状态下第一段可以点击排序)

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  if indexPath.section > 0 {
   // 更新数据
   let obj = recommendArr[indexPath.item]
   recommendArr.remove(at: indexPath.item)
   selectedArr.append(obj)
   //移动方法
   collectionView.moveItem(at: indexPath, to: NSIndexPath(item: selectedArr.count - 1, section: 0) as IndexPath)
  } else {
   if isEdite {
    if indexPath.item == 0 {return}
    // 更新数据
    let obj = selectedArr[indexPath.item]
    selectedArr.remove(at: indexPath.item)
    recommendArr.insert(obj, at: 0)
    //移动方法
    collectionView.moveItem(at: indexPath, to: NSIndexPath(item: 0, section: 1) as IndexPath)
   } else {
    if switchoverCallback != nil {
     //处理点击的闭包
     switchoverCallback!(selectedArr, recommendArr, indexPath.item)
     _ = navigationController?.popViewController(animated: true)
    }
   }
  }
 }
</div>

其他

此代码只是一个效果,没有怎么封装,如果仔细看过的朋友可以知道其实没有多么复杂

点击移动

collectionView.moveItem(at: indexPath, to: NSIndexPath(item: 0, section: 1) as IndexPath)
</div>

拖拽移动

collectionView.moveItem(at: indexPath!, to: targetIndexPath!)
</div>

主要就是这两个方法,其他都是处理逻辑以及视图效果.

提示

如果你们是从iOS9开始适配的话,那么可以用系统的Api,非常简单好用,大家这里可以自己去试试.

 // Support for reordering
 @available(iOS 9.0, *)
 open func beginInteractiveMovementForItem(at indexPath: IndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES
 @available(iOS 9.0, *)
 open func updateInteractiveMovementTargetPosition(_ targetPosition: CGPoint)
 @available(iOS 9.0, *)
 open func endInteractiveMovement()
 @available(iOS 9.0, *)
 open func cancelInteractiveMovement()
</div>

源码可以从这里下载

以上所述是小编给大家介绍的Swift下使用UICollectionView 实现长按拖拽功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Swift下使用UICollectionView 实现长按拖拽功能

相关文章

  • 2017-05-28Swift自定义iOS中的TabBarController并为其添加动画
  • 2017-05-28Mybatis接口式编程的原理
  • 2017-05-28Swift类型创建之自定义一个类型详解
  • 2017-05-28Swift中的Access Control权限控制介绍
  • 2017-05-28window下安装和配置maven环境
  • 2017-05-28JVM教程之内存管理和垃圾回收(三)
  • 2017-05-28Switch语句的技巧
  • 2017-05-28swift3.0指纹解锁的实现方法
  • 2017-05-28浅谈在Swift中关于函数指针的实现
  • 2017-05-22Swift 基本语法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Swift3.0剪切板代码拷贝及跨应用粘贴实现代码
    • 详解SimpleDateFormat的线程安全问题与解决方案
    • Swift教程之属性详解
    • 通过一个map替换字符串中指定的字符变量方法
    • 分隔List集合,按指定大小,将集合分成多个的方法
    • 解决 Xcode 6-Beta2 智能提示bug
    • Swift 3.0基础学习之闭包
    • Swift算法实现逐字翻转字符串的方法示例
    • Swift 访问控制
    • Swift教程之类与结构详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有