Drag and drop image in landscape doesn't work


Amadeu Andrade

I have the following code to drag and drop one image onto another.

import Foundation
import UIKit

class DragImg: UIImageView {

    var originalPosition: CGPoint!
    var dropTarget: UIView?

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        originalPosition = self.center
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.locationInView(self.superview)
            self.center = CGPointMake(position.x, position.y)
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if let touch = touches.first, let target = dropTarget {
            let position = touch.locationInView(self.superview)
            if CGRectContainsPoint(target.frame, position) {
                let notif = NSNotification(name: "onTargetDropped", object: nil)
                NSNotificationCenter.defaultCenter().postNotification(notif)
            }
        }
        self.center = originalPosition
    }   
}

This works fine in portrait mode, but if I change to landscape mode and try to drag and drop the image, the system doesn't recognize the image as being above the target image. Can you explain why and point to possible solutions?

Rob Mayov

I can't tell exactly what's going on without seeing more of the view hierarchy, but you put it this way selfand targethave different superviews. This means that their frames are in different coordinate systems. Each view defines its own coordinate system. These coordinate systems may occasionally align, and rotating the user interface may change whether the coordinate systems of the two views are aligned.

The frameview is meaningful only in the view's coordinate system superview. You are calculating in position = touch.locationInView(self.superview)the positioncoordinate system of self.superview. Then you're asking if CGRectContainsPoint(target.frame, position), but the target.framecoordinate system is different, so the answer usually doesn't make any sense.

The easiest way to see if it touchexists targetis:

let position = touch.locationInView(target)
if target.pointInside(position, withEvent: event) {
    // The touch is in the target.
}

Related


Drag and drop image in landscape doesn't work

Amadeu Andrade I have the following code to drag and drop one image onto another. import Foundation import UIKit class DragImg: UIImageView { var originalPosition: CGPoint! var dropTarget: UIView? override init(frame: CGRect) { super.ini

Drag and drop image in landscape doesn't work

Amadeu Andrade I have the following code to drag and drop one image onto another. import Foundation import UIKit class DragImg: UIImageView { var originalPosition: CGPoint! var dropTarget: UIView? override init(frame: CGRect) { super.ini

Drag and drop image into Word doesn't work

Georgia Road With MS Word 2000, I'm no longer able to drag and drop images from the local file system (or from images on the web) into MS Word. We recently reinstalled XP and SP3, so not sure if this is a new security feature (?). Currently, when I drag an ima

WebDriver drag and drop doesn't work on page

Vladimir: I have a very simple test @Test void dragAndDrop() { driver.get("https://www.testandquiz.com/selenium/testing.html"); driver.manage().window().maximize(); WebElement source = driver.findElement(By.id("sourceImage")); WebElement

Drag and drop in iframe doesn't work in Spring

Kroenke: I'm trying to implement drag and drop in an iframe in spring (working in Eclipse 2020-03) and the iframe doesn't seem to accept any cooperation possibilities. I ran into this - probably older - [working code snippet][1], but iframes in Eclipse can't e

Drag and drop doesn't work on Mac

Paul Lammertsma: I'm trying to drag files from Finder into my SWT application. On Windows and Ubuntu, the following code works: public class DndTest { public static void main(final String[] args) { final Display display = new Display(); fi

Drag and drop doesn't work in Xcode

Sam J. I'm using Xcode for my first Swift project and I'm having trouble dragging and dropping elements from a storyboard into code. When I try to drag and drop nothing happens. I'm running the latest version of Xcode and using Swift. The class is ViewControll

WebDriver drag and drop doesn't work on page

Vladimir: I have a very simple test @Test void dragAndDrop() { driver.get("https://www.testandquiz.com/selenium/testing.html"); driver.manage().window().maximize(); WebElement source = driver.findElement(By.id("sourceImage")); WebElement

Angular drag and drop doesn't work on mobile

Shreenivas I am using the lvlDragDrop plugin . It doesn't work on mobile platforms. They added this issue on github . But I still have no luck. Github demo HTML <div ng-controller="ddController" style="margin-top:50px;"> <div class="row">

Drag and drop doesn't work with headless chromedriver

Roubini I am using Selenium and Java to automate drag and drop operations. Below is the code used to perform drag and drop using chrome driver in headless mode. Point classname = targetElement.getLocation(); int xcordi = classname.getX(); int ycordi = classnam

Multiple drag and drop doesn't work

End of the World So far I've started with an image that I can drag into a box and drop it and it works fine. But I have a button that generates more images and when I put them on the same thing as the original, they don't go there. my code: var myCount = 1;

Drag and drop doesn't work on Mac

Paul Lammertsma: I'm trying to drag files from Finder into my SWT application. On Windows and Ubuntu, the following code works: public class DndTest { public static void main(final String[] args) { final Display display = new Display(); fi

Drag and drop in iframe doesn't work in Spring

Kroenke: I'm trying to implement drag and drop in an iframe in spring (working in Eclipse 2020-03) and the iframe doesn't seem to accept any cooperation possibilities. I ran into this - probably older - [working code snippet][1], but iframes in Eclipse can't e

Drag and drop to launcher doesn't work

Ahmed Based on this how to add a shell script to the launcher as a shortcut I moved the file .desktopto . .local\share\applicationsAnyway , it told me I had to drag and drop it into the launcher. But it didn't respond or open a new location in the launcher, so

Drag and drop doesn't work in Xcode

Sam J. I'm using Xcode for my first Swift project and I'm having trouble dragging and dropping elements from a storyboard into code. When I try to drag and drop nothing happens. I'm running the latest version of Xcode and using Swift. The class is ViewControll

Angular drag and drop doesn't work on mobile

Shreenivas I am using the lvlDragDrop plugin . It doesn't work on mobile platforms. They added this issue on github . But I still have no luck. Github demo HTML <div ng-controller="ddController" style="margin-top:50px;"> <div class="row">

Gridster drag and drop doesn't work

sivs JSFiddle example : https://jsfiddle.net/27t0jymr/ <section id="mainContainer"> <!-- Dashboard Section --> <div class="dashboard"> <div class="gridster"> <ul> <li data-row="1" data-col="1" data-sizex="2" data-sizey="2"> <h1>

Drag and drop doesn't work with headless chromedriver

Roubini I am using Selenium and Java to automate drag and drop operations. Below is the code used to perform drag and drop using chrome driver in headless mode. Point classname = targetElement.getLocation(); int xcordi = classname.getX(); int ycordi = classnam

Drag and drop photo preview doesn't work

cool rock Photo preview not working please help me //single photo upload with box click //keyValPImage stores the box image in array $(".dzq-img-box ") .on( 'click', this, function() { $('<input type="file" name="files[]" />') .click() .on(

Drag and drop doesn't work in Xcode

Sam J. I'm using Xcode for my first Swift project and I'm having trouble dragging and dropping elements from the storyboard into the code. When I try to drag and drop, nothing happens. I'm running the latest version of Xcode and using Swift. The class is ViewC

Drag and drop photo preview doesn't work

cool rock Photo preview not working please help me //single photo upload with box click //keyValPImage stores the box image in array $(".dzq-img-box ") .on( 'click', this, function() { $('<input type="file" name="files[]" />') .click() .on(

Angular drag and drop doesn't work on mobile

Shrinivas pai I am using the lvlDragDrop plugin . It doesn't work on mobile platforms. On github they added this issue . But I still have no luck. Github demo HTML <div ng-controller="ddController" style="margin-top:50px;"> <div class="row">

13.10 Drag and drop doesn't work

Vitor Mazuco I can't drag and drop items to Ubuntu 13.10 destop. It keeps giving me this error. Erro ao Copiar. erro ao copiar. houve um erro ao obter informação sobre "/". English: Error in Copying There was an error getting information about "/". Jack This

Drag and drop doesn't work in Chrome

username Iam trying to use drag and drop. In the anchor tag I wrote onsdragstart event <a href="xyz.action" ondragstart="event.dataTransfer.setData('text/uri-list', 'abc.action?id=2')">drag</a> I am trying to put the item in a div <div id="ccc" ondragover="

Drag and drop doesn't work in IE

because I have a list of units that I want to drag and drop onto an SVG canvas. You can see it here . Define and configure units for this operation by: var units = html.document.querySelectorAll('ul.units li'); units.onDragStart.listen(_onDragStart); units.onD

Drag and drop doesn't work in C#

Kieran Crown I created a drag and drop control in C# to allow people to drag and drop files onto my form. This is the problem I'm having, it works fine when debugging; however, it doesn't work when running the program in admin mode. Is there any reason? Here i

Drag and drop functionality doesn't work?

Shayan Razavi I'm a beginner in Javascript and I'm trying to get an image to go inside a border made by CSS, and for some reason the script doesn't work, maybe I made a small mistake in the code or something else. The code snippet works fine but the image is n

13.10 Drag and drop doesn't work

Vitor Mazuco I can't drag and drop items to Ubuntu 13.10 destop. It keeps giving me this error. Erro ao Copiar. erro ao copiar. houve um erro ao obter informação sobre "/". English: Error in Copying There was an error getting information about "/". Jack This

WebDriver drag and drop doesn't work on page

Vladimir: I have a very simple test @Test void dragAndDrop() { driver.get("https://www.testandquiz.com/selenium/testing.html"); driver.manage().window().maximize(); WebElement source = driver.findElement(By.id("sourceImage")); WebElement