0

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

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

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

-, +

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

-, -

再次向左旋转:

+, -

再次向左旋转:

+, +

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

任何建议将不胜感激。

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

1 回答 1

0

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

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;
}
于 2013-10-27T04:55:28.220 回答