1

I am using these pieces of code to make coordinates global.

    let referenceView = self.view
    let dropArea = referenceView.convertRect(self.dropAreaView1.bounds, fromView: self.dropAreaView1)
    let center = referenceView.convertPoint(sender.view!.center, fromView: sender.view!.superview)

And I am using this piece of code to drop the UIView to the center of another UIView. Drag and drop action work but it never drops on the center.

    sender.view!.frame = CGRectMake(dropArea.origin.x , dropArea.origin.y, sender.view!.frame.width * 0.6, sender.view!.frame.height * 0.6)

The whole code is

func draggedView(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(self.view)

    let referenceView = self.view
    let dropArea = referenceView.convertRect(self.dropAreaView1.bounds, fromView: self.dropAreaView1)
    let center = referenceView.convertPoint(sender.view!.center, fromView: sender.view!.superview)

    switch sender.state {
    case UIGestureRecognizerState.Began:

        originCenter = sender.view!.center

    case UIGestureRecognizerState.Ended:
        UIView.animateWithDuration(0.3, animations: { () -> Void in

            if CGRectContainsPoint(dropArea, center) {

                sender.view!.frame = CGRectMake(sender.view!.frame.origin.x, sender.view!.frame.origin.y, sender.view!.frame.width * 0.6, sender.view!.frame.height * 0.6)

            }
    }
Mamedoff
  • 195
  • 1
  • 2
  • 13
  • What happens when you set `sender.view!.center = CGPoint(x: dropArea2.midX, y: dropArea2.midY)`? – beyowulf Aug 18 '16 at 22:27
  • sender.view goes away from the view :( – Mamedoff Aug 18 '16 at 22:47
  • Please show much more code, e.g. it's not clear what `sender` is. – Codo Aug 19 '16 at 05:57
  • http://s4.postimg.io/53hbbanj1/Screen_Shot_2016_08_19_at_12_45_07.jpg. Sender is the UIView I am dragging. – Mamedoff Aug 19 '16 at 08:47
  • Please edit the question and add the code their as real text (instead of as an image). See http://meta.stackoverflow.com/a/307500/413337 – Codo Aug 19 '16 at 10:36
  • I did. Please check it out now. – Mamedoff Aug 19 '16 at 10:49
  • I'm afraid to have to provide much more information about the problem. Even with the information in your original [question](http://stackoverflow.com/questions/38957319/cgrectcontainspoint-does-not-work-on-different-views/38958093?noredirect=1#comment65421940_38958093), which you should add to this question, I don't understand what you're trying to achieve overall, what the view hierarchy looks like, which view is moved, which one you're testing against, which view has the gesture recognizer etc. – Codo Aug 19 '16 at 11:01
  • And when you convert it `sender.view!.center = referenceView.convertPoint(CGPoint(x: dropArea2.midX, y: dropArea2.midY), toView: sender.view!.superview)`? – beyowulf Aug 19 '16 at 12:44
  • Thank you so very much beyowulf. It worked. – Mamedoff Aug 19 '16 at 16:22

0 Answers0