简体   繁体   中英

Tap and drag from one UITable to another below it

I have two UITables, one above the other. User can tap and drag an element from top table to bottom. When the element is being dragged over the bottom table, it disappears.

This problem did not occur when the tables were side by side.

A clue/symptom could be; when you are scrolling the top table, if you continue to move your finger down past the top table and over the bottom one, the top table continues to scroll. It is like it is behaving like the table extends all the way to the bottom of the screen, even though it doesn't.

The problem also seems to happen only on 4" screens.

Here is the drag and drop function:

    func dragAndDrop( pangesture : UIPanGestureRecognizer)
{
    print("dragAndDrop \(pangesture.state.rawValue) section=\(section)  row=\(row)")
    if(pangesture.state == UIGestureRecognizerState.Ended)
    {
        if(section == nil)
        {
            return
        }

        let locationInView : CGPoint! = pangesture.locationInView(self.tbldrag)
        if (CGRectContainsPoint(self.tbldrag.bounds, locationInView))
        {
            var moods : [Mood] = getMoodsWithRO(mClient.getRooms()[self.section].getRoomID())

            //dropped to To table
            self.arrDroproom.append(mClient.getRooms()[self.section].getName())

            var temp:Mood = moods[self.row]

            if(moods[self.row].getMoodName() == "Space Off")
            {
                self.arrDropdata.append(mClient.getRooms()[self.section].getRoomID())
            }
            else
            {
                self.arrDropdata.append(moods[self.row].getMoodID())
            }

            self.arrDropmood.append(moods[self.row].getMoodName())
            self.cellDrag.removeFromSuperview()
            self.tblMoods.reloadData()
            self.tbldrag.reloadData()

            addDelayButton.enabled = true

            return
        }
        self.section = nil
        self.row = nil
        if((self.cellDrag) != nil)
        {
            self.cellDrag.removeFromSuperview()
        }
        self.tblMoods.reloadData()
        self.tbldrag.reloadData()
        self.isHorizontalGeasture = false
    }
    else if(pangesture.state == UIGestureRecognizerState.Began)
    {
        let vel : CGPoint = pangesture.velocityInView(self.tblMoods)
        //println(vel)

        let isVerticalGesture : Bool = fabs(vel.y) > fabs(vel.x)

        var a = "horiz"
        if(isVerticalGesture)
        {
            a="vert"
        }
        print("began: gesture is \(a)")


        if((UIDevice.currentDevice().userInterfaceIdiom != .Phone && isVerticalGesture==false) || (UIDevice.currentDevice().userInterfaceIdiom == .Phone))
        {
            self.isHorizontalGeasture = !isVerticalGesture
            if (vel.x > 0)
            {
                //NSLog("towards the right")
            }
            else
            {
                //NSLog("towards the left")
            }
            let touchLocation1 : CGPoint! = pangesture.locationInView(self.tblMoods)

            let indexPath : NSIndexPath? = self.tblMoods.indexPathForRowAtPoint(touchLocation1)

            if(indexPath != nil)
            {
                self.section = indexPath!.section
                self.row = indexPath!.row
                self.cellDrag = self.tblMoods.cellForRowAtIndexPath(indexPath!)!

                self.view.addSubview(self.cellDrag)
            }
        }
    }
    else
    {
        if((UIDevice.currentDevice().userInterfaceIdiom != .Phone && self.isHorizontalGeasture) || (UIDevice.currentDevice().userInterfaceIdiom == .Phone))
        {
            let touchLocation : CGPoint! = pangesture.locationInView(self.view)

            self.cellDrag.center = touchLocation
        }
        else
        {
            pangesture.delegate = nil
        }

    }
}

I found my answer: When the problem happens it sends a Cancelled event.

"You should almost always use UIGestureRecognizerState.Ended || UIGestureRecognizerState.Cancelled as one of the two will definitely be called at the end of a gesture. This way you can also handle cases where the user has dragged past the screen."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM