简体   繁体   English

ios随设备旋转拖放

[英]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;
}

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

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