简体   繁体   中英

Issue with Dropping point in Swift

I have a table view and outside the table view and i have one view which is outside the table view. I am trying to drag and drop elements from table view to Drop view. The View looks something like this.

在此处输入图片说明

There is a Table View as shown in the figure. What i am trying to accomplish is, when the user long presses on a row in table view i need to drag and drop the selected item into the droppable area. On long pressing i create a snapshot of the row and add as a subview, now i am trying to drag the subview into the drop area. I am not able to do this. Can anyone help me out with this issue.

  var stateDropBoard = CGRectContainsPoint(ingBoardDropView.frame, touchPoint)

    if(stateDropBoard)
    {
       print("DROPPED")            
    }

I am not able to get it working. Is there any way to accomplish what i am doing.

On your parent view use touches ended method. Inside this method run the test to check if user has dropped item on top of your green colour view.

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

      CGPoint location = [[touches anyObject] locationInView:self.view];
      CGRect fingerRect = CGRectMake(location.x-5, location.y-5, 10, 10);

      for(UIView *view in self.view.subviews){
         CGRect subviewFrame = view.frame;

         if(CGRectIntersectsRect(fingerRect, subviewFrame)){
            // this is your touched view
        }

      }

   }

Here is swift version which you asked! with little optimisation.

 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first! as UITouch
        let location = touch.locationInView(self.view)
        let hitTest =  self.targetView.hitTest(location, withEvent:event )

        if (hitTest != nil) {
            print("User finished touch inside your target view!")
        }else{
            print("User finished touch outside your target view");
        }

    }

Note : Make sure you have correct constraints in place for your target view.

I guess you can take it from here. Hope this helps.

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