美文网首页
swift 拖动collectionCell并改变其位置和数据位

swift 拖动collectionCell并改变其位置和数据位

作者: MrLSX | 来源:发表于2018-10-10 15:13 被阅读0次
老规矩 先上图: QQ20181010-151146-HD.gif

1.为cell添加长按手势

let tap = UILongPressGestureRecognizer(target: self, action: #selector(delectBtn(_:)))
tap.minimumPressDuration = 1
tap.numberOfTouchesRequired = 1
cell.addGestureRecognizer(tap)

2.为长按手势添加事件

@objc func delectBtn(_ sender: UILongPressGestureRecognizer) {
    switch sender.state {
    case .began:
       //取得要移动cell的索引
       let selectedCellIndex = collectionView.indexPathForItem(at: sender.location(in: collectionView))
        //开始移动
            collectionView.beginInteractiveMovementForItem(at: selectedCellIndex!)
    case .changed:
        //移动ing
        collectionView.updateInteractiveMovementTargetPosition(sender.location(in: collectionView))
    case .ended:
        //结束移动
        collectionView.endInteractiveMovement()
    default:
        //取消移动
        collectionView.cancelInteractiveMovement()
    }
    
}

3.处理移动后数据的改变

//sourceIndexPath.item移动前cell的索引
//destinationIndexPath.item移动后的索引
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let tempMybook = mybook[sourceIndexPath.item]
    mybook.remove(at: sourceIndexPath.item)
    mybook.insert(tempMybook, at: destinationIndexPath.item)
}

相关文章

  • swift 拖动collectionCell并改变其位置和数据位

    1.为cell添加长按手势 2.为长按手势添加事件 3.处理移动后数据的改变

  • UISlider-改变图片透明度

    拖动条是通过滑块的位置来标识数值,而且拖动条允许用户拖动滑块来改变值。因此,拖动条通常用于对系统的某种数值进行调节...

  • 7.2.2 实战:用实色渐变制作水晶按钮

    1、选择渐变颜色,渐变工具 2、打开渐变编辑器 3、调整渐变颜色,拖动色标,改变颜色位置 4、拖动动点改变颜色的混...

  • 调用百度地图API,实现两种效果

    效果一:地图拖动,中心定位标注不动,准确实时获取地图拖动后的中心点位置,并获取中心点位置一定区域内的十个具体位置。...

  • CentOS7 中 yum 安装 MongoDB

    1. 创建文件 2. 安装 默认数据位置 默认日志位置 配置文件位置 修改监听ip和端口 3. 开放3717端口 ...

  • docker 使用总结 win10

    docker的数据位置及安装位置 docker默认的安装位置是在:"C:\Program Files\Docker...

  • keras下载数据位置

    在程序中,经常会用到 实际的下载路径在 一个隐藏目录下

  • 几个常用的chrome调试技巧

    1. 拖动 dom 元素 选中一个dom元素,通过拖动就可以改变元素的位置。如下图: 2. 选中 dom 元素右键...

  • C4D三维动画

    基本操作 选择:可以选择多个元素,但是不能拖动移动:可以拖动某个元素移动其位置层级:元素之间也可以存着上下层关系,...

  • PS基础干货总结

    矢量: 1、矢量——放大缩小不会模糊失真 按住command键,拖动点改变锚点位置,拖动线可以变成平行四边形(同时...

网友评论

      本文标题:swift 拖动collectionCell并改变其位置和数据位

      本文链接:https://www.haomeiwen.com/subject/xjwpaftx.html