簡體   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