繁体   English   中英

ios随设备旋转拖放

[英]ios Drag & Drop with Device Rotation

我使用以下代码(如下所示)在多个不同位置实现拖放操作,并且过去一直很适合我。

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

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

-,+

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

-,-

再次向左旋转:

+,-

再次向左旋转:

+,+

我完全不了解这里发生的情况,特别是因为此代码似乎在其他视图控制器中运行良好。

任何建议将不胜感激。

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

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

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