简体   繁体   中英

ios Drag & Drop with Device Rotation

I've used the following code, shown below, to implement drag & drop in several different places and has always worked well for me in the past.

Now I have a problem with it and have no idea why. It works perfectly so long as I have the device (or simulator) oriented portrait with the button down. But in any of the other three orientations, as the finger dragging the view moves one direction, the "dragged" view moves a different direction.

As shown below I'm logging the value of translation with each move. In the original orientation, as I drag from the middle of the screen toward the lower-left corner, the values for translation are:

-, +

If I rotate left, and do it again:

-, -

Rotate left again:

+, -

Rotate left again:

+, +

I am totally not getting what happens here, particularly since this code seems to work well in other view controllers.

Any suggestions will be appreciated.

- (void) didMakePanGesture:(UIPanGestureRecognizer *)panGesture
{
    if (panGesture.state == UIGestureRecognizerStateBegan)
    {
        [self setDropTargetsCoordinates];                                                           // saves correct drop target & its coordinates
        dragViewStartLocation = receptiveClassificationImageView.center;                            // save center in case we have to snap back
        receptiveClassificationImageView.transform = CGAffineTransformMakeScale(0.40f, 0.40f);      // make the image smaller for dragging
        receptiveClassificationImageView.layer.cornerRadius = 12.0f;                                // we lose the rounded corners in the scaling; this to fix
    }
    else if (panGesture.state == UIGestureRecognizerStateChanged)
    {
        //
        //   Adjust the location of the dragged view whenever state changes
        //
        CGPoint translation = [panGesture translationInView:nil];
        CGAffineTransform transform = receptiveClassificationImageView.transform;
        transform.tx = translation.x;
        transform.ty = translation.y;
        receptiveClassificationImageView.transform = transform;
        NSLog(@"Translation=%f,%f" translation.x, translation.y);


    }
    else if (panGesture.state == UIGestureRecognizerStateEnded)
    {
        // do stuff when dropped   
    }

Just in case someone sees this later, this problem was solved with the following changes to the code above:

if (panGesture.state == UIGestureRecognizerStateBegan)
{
    [self setDropTargetsCoordinates];                                                           // saves correct drop target & its coordinates
    dragViewStartLocation = receptiveClassificationImageView.center;                            // save center in case we have to snap back
    **receptiveClassificationImageView.center = [panGesture locationInView:receptiveClassificationImageView.superview]; // re-center the view before scaling**
    receptiveClassificationImageView.transform = CGAffineTransformMakeScale(0.40f, 0.40f);      // make the image smaller for dragging
    receptiveClassificationImageView.layer.cornerRadius = 12.0f;                                // we lose the rounded corners in the scaling; this to fix
}
else if (panGesture.state == UIGestureRecognizerStateChanged)
{
    //
    //   Adjust the location of the dragged view whenever state changes
    //
    CGPoint translation = [panGesture translationInView:**self.view**];
    CGAffineTransform transform = receptiveClassificationImageView.transform;
    transform.tx = translation.x;
    transform.ty = translation.y;
    receptiveClassificationImageView.transform = transform;
}

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