搜集知识我的ios进阶iOS技术专题

iOS7+ UICollectionView拖拽重排

2016-06-23  本文已影响1234人  文兴

UIcollectionView是开发中最常使用到的组件之一,然而其拖拽排序的功能直到iOS9才引入,iOS9以前的版本并没有原生的支持。为此我写了一个小型库 SortableCollectionView,方便地集成拖拽排序的功能。项目地址https://github.com/luowenxing/SortableCollectionView 只需要直接使用或者子类化SortableCollectionView,并实现简单的代理方法,就能方便地实现拖拽排序。同时支持自定义拖拽的视图,比如对拖拽中的Cell进行放大、设置背景颜色等等。

效果展示

使用

@objc protocol SortableCollectionViewDelegate:NSObjectProtocol {
    // 排序开始,定制拖拽排序的视图 
    optional func beginDragAndInitDragCell(collectionView:SortableCollectionView,dragCell:UIView)
    
    // 排序结束,重设排序视图
    optional func endDragAndResetDragCell(collectionView:SortableCollectionView,dragCell:UIView)
    
    // 排序结束,对排序完成后的真实Cell进行操作,比如类似支付宝的首页排序,对没有移动的Cell显示删除按钮
    optional func endDragAndOperateRealCell(collectionView:SortableCollectionView,realCell:UICollectionViewCell,isMoved:Bool)
    
    // 排序完成,交换数据源
    func exchangeDataSource(fromIndex:NSIndexPath,toIndex:NSIndexPath)
}
func collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool 
func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) 

则会使用系统原生的拖拽排序,目前原生排序是一个简单的版本,不支持自定义的拖拽view。你可以在Demo里面试一试,比较两种方式。

原理分析

    func scrollAtEdge(){
        //计算拖动视图里边缘的距离,正比于滚动速度,并且判断是往上还是往下滚动
        let pinTop = dragView.frame.origin.y
        let pinBottom = self.frame.height - (dragView.frame.origin.y + dragView.frame.height)
        var speed:CGFloat = 0
        var isTop:Bool = true
        if pinTop < 0 {
            speed = -pinTop
            isTop = true
        } else if pinBottom < 0 {
            speed = -pinBottom
            isTop = false
        } else {
            self.timer?.invalidate()
            self.timer = nil
            return
        }
        if let originTimer = self.timer,originSpeed = (originTimer.userInfo as? [String:AnyObject])?["speed"] as? CGFloat{
            //计算滚动速度和原来相差是否过大,目的是防止频繁的创建定时器而使滚动卡顿
            if abs(speed - originSpeed) > 10 {
                originTimer.invalidate()
                NSLog("speed:\(speed)")
                // 60fps,滚动才能流畅
                let timer = NSTimer(timeInterval: 1/60.0, target: self, selector: #selector(SortableCollectionView.autoScroll(_:)), userInfo: ["top":isTop,"speed": speed] , repeats: true)
                self.timer = timer
                NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
            }
        } else {
            let timer = NSTimer(timeInterval: 1/60.0, target: self, selector: #selector(SortableCollectionView.autoScroll(_:)), userInfo: ["top":isTop,"speed": speed] , repeats: true)
            self.timer = timer
            NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
        }
    }
    
    
    func autoScroll(timer:NSTimer) {
        if let userInfo = timer.userInfo as? [String:AnyObject] {
            if let top =  userInfo["top"] as? Bool,speed = userInfo["speed"] as? CGFloat {
                //计算滚动位置,更新contentOffset
                let offset = speed / 5
                let contentOffset = self.contentOffset
                if top {
                    self.contentOffset.y -= offset
                    self.contentOffset.y = self.contentOffset.y < 0 ? 0 : self.contentOffset.y
                }else {
                    self.contentOffset.y += offset
                    self.contentOffset.y = self.contentOffset.y > self.contentSize.height - self.frame.height ? self.contentSize.height - self.frame.height  : self.contentOffset.y
                }
                let point = CGPoint(x: dragView.center.x, y: dragView.center.y + contentOffset.y)
                //滚动过程中,拖拽视图位置不变,因此手势识别代理不会调用,需要手动调用移动item
                self.moveItemToPoint(point)
            }
        }
    }

待改进

上一篇 下一篇

猜你喜欢

热点阅读