【问题标题】:ios Drag & Drop with Device Rotationios 拖放与设备旋转
【发布时间】:2013-10-26 23:04:53
【问题描述】:

我使用下面的代码(如下所示)在几个不同的地方实现拖放,并且过去一直对我很有效。

现在我遇到了问题,不知道为什么。只要我有面向设备(或模拟器)的肖像并按下按钮,它就可以完美地工作。但是在其他三个方向中的任何一个中,当拖动视图的手指移动一个方向时,“拖动”的视图移动一个不同的方向。

如下所示,我正在记录每次移动的平移值。在原始方向上,当我从屏幕中间向左下角拖动时,平移的值为:

-, +

如果我向左旋转,然后再做一次:

-,-

再次向左旋转:

+, -

再次向左旋转:

+, +

我完全不明白这里发生了什么,特别是因为这段代码似乎在其他视图控制器中运行良好。

任何建议将不胜感激。

- (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   
    }

【问题讨论】:

    标签: ios drag-and-drop


    【解决方案1】:

    以防万一以后有人看到这个问题,通过对上面的代码进行以下更改解决了这个问题:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2021-08-27
      • 1970-01-01
      相关资源
      最近更新 更多