简体   繁体   English

Swift 中的滴点问题

[英]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.我正在尝试将元素从表视图拖放到 Drop 视图。 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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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