欢迎光临
我们一直在努力

[ Swift ] 单元格,乱插,模仿今日头条

建站超值云服务器,限时71元/月

[ Swift ] 单元格,乱插,模仿今日头条

项目介绍:

[Objective-C] 查看源文件 复制代码

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if !self.isEdit {
            super.touchesBegan(touches, withEvent: event)
            return
        }
        let touch:CGPoint! = event?.allTouches()?.first?.locationInView(self);
        let flowLayout = self.collectionViewLayout as! EditFlowLayout

        for (item,attributes) in flowLayout.attributess.enumerate() {
            if CGRectContainsPoint(attributes.frame, touch) {
                self.moveItem = item
                self.fromIndexPath = NSIndexPath.init(forItem: self.moveItem!, inSection: 0)
                self.toIndexPath = NSIndexPath.init(forItem: self.moveItem!, inSection: 0)
                let indexPath = NSIndexPath.init(forItem: item, inSection: 0)
                let moveCell: CollectionViewCell = self.cellForItemAtIndexPath(indexPath) as! CollectionViewCell
                self.distanceX = touch.x - moveCell.center.x
                self.distanceY = touch.y - moveCell.center.y
                self.copyView = moveCell.mutableCopy() as? CollectionViewCell
                moveCell.superview?.addSubview(self.copyView!)
                //对cell进行处理,直接设置cell透明度不能达到效果
                moveCell.backgroundColor = UIColor.clearColor()
                for view in moveCell.subviews {
                    view.alpha = 0.0
                }
//                moveCell.alpha = 0
            }
        }

    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if !self.isEdit {
            super.touchesMoved(touches, withEvent: event)
            return
        }
        let touch:CGPoint! = event?.allTouches()?.first?.locationInView(self);

        if let copyView = self.copyView {
            dispatch_async(dispatch_get_main_queue(), {
                copyView.center.x = touch.x - self.distanceX!
                copyView.center.y = touch.y - self.distanceY!
            })
            let flowLayout = self.collectionViewLayout as! EditFlowLayout
            var number:Int! = 0
            for (item,attributes) in flowLayout.attributess.enumerate() {
                number = number + 1
                if item == self.moveItem {
                    continue
                }
                let attributesWidth = attributes.bounds.size.width
                let attributesHeight = attributes.bounds.size.height
                let copyViewWidth = copyView.bounds.size.width
                let maxWidth = attributesWidth > copyViewWidth ? attributesWidth:copyViewWidth
//                  let copyViewHeight = copyView.bounds.size.height

                if fabs(copyView.center.x - attributes.center.x) < (maxWidth - fabs(attributesWidth - copyViewWidth))/2 {
                    if fabs(copyView.center.y - attributes.center.y) < attributesHeight/2 {
                        self.fromIndexPath = NSIndexPath.init(forItem: self.moveItem!, inSection: 0)
                        self.toIndexPath = NSIndexPath.init(forItem: item, inSection: 0)
                        flowLayout.moveIndexPath = self.fromIndexPath
                        if self.fromIndexPath?.item == self.toIndexPath?.item {
                            return
                        }
                        dispatch_async(dispatch_get_global_queue(0, 0), {
                        NSNotificationCenter.defaultCenter().postNotificationName(MoveCellNotificationName, object: nil, userInfo: ["fromIndexPath": self.fromIndexPath!,"toIndexPath": self.toIndexPath!])

                        })
                        self.moveItem = item;
                        break
                    }
                }
            }
            if number >= flowLayout.attributess.count {

                let desAttributes: UICollectionViewLayoutAttributes = flowLayout.attributess[number - 1]
                let moveCellMinX = self.copyView!.center.x - self.copyView!.bounds.size.width/2
                let moveCellMinY = self.copyView!.center.y - self.copyView!.bounds.size.height/2
                let desCellMinX = desAttributes.center.x - desAttributes.bounds.size.width/2
                let desCellMaxY = desAttributes.center.y + desAttributes.bounds.size.height/2
//                print("\(moveCellMinX)--\(moveCellMinY)--\(desCellMinX)--\(desCellMaxY)")

                if (moveCellMinX > desCellMinX && moveCellMinY > desCellMaxY) || moveCellMinY > desCellMaxY{
                    self.fromIndexPath = NSIndexPath.init(forItem: self.moveItem!, inSection: 0)
                    self.toIndexPath = NSIndexPath.init(forItem: number - 1, inSection: 0)
                    flowLayout.moveIndexPath = self.fromIndexPath
                    if self.fromIndexPath?.item == self.toIndexPath?.item {
                        return
                    }
                    dispatch_async(dispatch_get_global_queue(0, 0), {
                        NSNotificationCenter.defaultCenter().postNotificationName(MoveCellNotificationName, object: nil, userInfo: ["fromIndexPath": self.fromIndexPath!,"toIndexPath": self.toIndexPath!])
                    })
                    self.moveItem = number - 1;
                }
            }
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if !self.isEdit {
            super.touchesEnded(touches, withEvent: event)
            return
        }
        if let indexPath = self.toIndexPath {
            let cell:CollectionViewCell = self.cellForItemAtIndexPath(toIndexPath!) as! CollectionViewCell;
            let flowLayout = self.collectionViewLayout as! EditFlowLayout
            let attributes:UICollectionViewLayoutAttributes = flowLayout.attributess[toIndexPath!.item]
            UIView.animateWithDuration(0.3, animations: {
                self.copyView!.frame = attributes.frame
            }) { (completed) in
                //对cell进行处理,直接设置cell透明度不能达到效果
                cell.backgroundColor = UIColor.redColor()
                for view in cell.subviews {
                    view.alpha = 1.0
                }
                //                cell.alpha = 1.0
                self.copyView!.removeFromSuperview()
                self.copyView = nil
                self.toIndexPath = nil
                self.fromIndexPath = nil
                flowLayout.moveIndexPath = nil;
            }

        } else {
            if let from = self.fromIndexPath {

            } else {
                return
            }
            let cell:CollectionViewCell = self.cellForItemAtIndexPath(fromIndexPath!) as! CollectionViewCell;
            let flowLayout = self.collectionViewLayout as! EditFlowLayout
            let attributes:UICollectionViewLayoutAttributes = flowLayout.attributess[fromIndexPath!.item]

            self.copyView!.frame = attributes.frame
            //对cell进行处理,直接设置cell透明度不能达到效果
            cell.backgroundColor = UIColor.redColor()
            for view in cell.subviews {
                view.alpha = 1.0
            }
            //                cell.alpha = 1.0
            self.copyView!.removeFromSuperview()
            self.copyView = nil
            self.toIndexPath = nil
            self.fromIndexPath = nil
赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » [ Swift ] 单元格,乱插,模仿今日头条
分享到: 更多 (0)