集合视图拖放

时间:2016-07-18 07:07:23

标签: ios swift ios9 uicollectionviewcell

我想在两个集合视图之间实现拖放功能。为此我使用KDDragAndDropManager.swift。当我的项目与其他项目的集合视图交叉时,这工作正常。但我想拖动一个新的位置,其中单元格不与任何其他单元格相交。前几天我被困在这里。请帮助任何帮助

func updateForLongPress(recogniser : UILongPressGestureRecognizer) -> Void {

            if let bundl = self.bundle {

                let pointOnCanvas = recogniser.locationInView(recogniser.view)
                let sourceDraggable : KDDraggable = bundl.sourceDraggableView as! KDDraggable
                let pointOnSourceDraggable = recogniser.locationInView(bundl.sourceDraggableView)

                switch recogniser.state {


                case .Began :
                    self.canvas.addSubview(bundl.representationImageView)
                    sourceDraggable.startDraggingAtPoint?(pointOnSourceDraggable)

                case .Changed :

                    // Update the frame of the representation image
                    var repImgFrame = bundl.representationImageView.frame
                    repImgFrame.origin = CGPointMake(pointOnCanvas.x - bundl.offset.x, pointOnCanvas.y - bundl.offset.y);
                    bundl.representationImageView.frame = repImgFrame

                    var overlappingArea : CGFloat = 0.0

                    var mainOverView : UIView?

                    for view in self.views.filter({ v -> Bool in v is KDDroppable }) {

                        let viewFrameOnCanvas = self.convertRectToCanvas(view.frame, fromView: view)


                        let intersectionNew = CGRectIntersection(bundl.representationImageView.frame, viewFrameOnCanvas).size


                        if (intersectionNew.width * intersectionNew.height) > overlappingArea {

                            overlappingArea = intersectionNew.width * intersectionNew.width

                            mainOverView = view
                        }


                    }



                    if let droppable = mainOverView as? KDDroppable {

                        let rect = self.canvas.convertRect(bundl.representationImageView.frame, toView: mainOverView)

                        if droppable.canDropAtRect(rect) {

                            if mainOverView != bundl.overDroppableView { // if it is the first time we are entering

                                (bundl.overDroppableView as! KDDroppable).didMoveOutItem(bundl.dataItem)
                                droppable.willMoveItem(bundl.dataItem, inRect: rect)

                            }

                            // set the view the dragged element is over
                            self.bundle!.overDroppableView = mainOverView

                            droppable.didMoveItem(bundl.dataItem, inRect: rect)

                        }


                    }


                case .Ended :

                    if bundl.sourceDraggableView != bundl.overDroppableView { // if we are actually dropping over a new view.

                        print("\(bundl.overDroppableView?.tag)")

                        if let droppable = bundl.overDroppableView as? KDDroppable {

                            sourceDraggable.dragDataItem(bundl.dataItem)

                            let rect = self.canvas.convertRect(bundl.representationImageView.frame, toView: bundl.overDroppableView)

                            droppable.dropDataItem(bundl.dataItem, atRect: rect)

                        }
                    }


                    bundl.representationImageView.removeFromSuperview()
                    sourceDraggable.stopDragging?()

                default:
                    break

                }


            } // if let bundl = self.bundle ...



        }

0 个答案:

没有答案