簡體   English   中英

泛手勢-滑動手勢沖突

[英]Pan Gesture - Swipe Gesture Conflict

我正在嘗試創建一個應用程序,該應用程序可以復制Apple的照片應用程序(iPhone)縮放,平移和滾動照片圖像的功能。 (在查看pdf和其他文檔時,我也想使用相同的控件。)我得到了點擊手勢來顯示/隱藏導航欄,並且獲得了滑動手勢來從左向右滾動圖像,反之亦然。 然后,我得到了放大和縮小的捏合手勢,但是當我添加平移手勢以在縮放的圖像中四處移動時,滑動手勢就停止了工作。

我在StackOverflow的其他地方找到了可能的解決方案,包括使用shouldRecognizeSimultaneouslyWithGestureRecognizer ,但是到目前為止,我還無法解決沖突。 有什么建議么?

這是代碼:

func gestureRecognizer(UIPanGestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer UISwipeGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

@IBAction func handlePinch(sender: UIPinchGestureRecognizer) {

    sender.view!.transform = CGAffineTransformScale(sender.view!.transform, sender.scale, sender.scale)
    sender.scale = 1
}

@IBAction func handlePan(sender: UIPanGestureRecognizer) {

    self.view.bringSubviewToFront(sender.view!)
    var translation = sender.translationInView(self.view)
    sender.view!.center = CGPointMake(sender.view!.center.x + translation.x, sender.view!.center.y + translation.y)
    sender.setTranslation(CGPointZero, inView: self.view)
}

@IBAction func handleSwipeRight(sender: UISwipeGestureRecognizer) {

    if (self.index == 0) {
    self.index = ((photos.count) - 1);
    }
    else
    {
    self.index--;
    }

    // requireGestureRecognizerToFail(panGesture)

    setImage()
}

您不希望shouldRecognizeSimultaneouslyWithGestureRecognizer:允許兩個手勢同時發生)。 例如,如果要同時捏和平移,這很有用。 但是,在您同時搖動和滑動的情況下,同時手勢將無濟於事。 (如果有的話,同時識別它們可能會使情況混亂。)

相反,您可能想使用requireGestureRecognizerToFail:建立滑動和平移手勢的優先級(例如,只有在滑動失敗時才平移)。

或更妙的是,完全撤消滑動手勢,而僅使用平移手勢,如果將其縮小,則將是一種交互式手勢,可以從一個圖像導航到下一個圖像,如果放大,則可以平移圖像。 無論如何,交互式平移手勢通常會更令人滿意。 例如,如果從一張照片滑動到另一張照片,則可以停止平移手勢並返回。 如果查看Photos.app,它實際上是使用平移手勢從一個圖像滑動到另一圖像,而不是滑動手勢。

我在http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift上找到了一個教程,該教程很好地介紹了UIScrollView,它是Swift中縮放,平移和分頁的一種組合方式。 我建議所有嘗試學習如何使這些手勢一起正常工作的人使用它。

在類似情況下,我使用了另一種方法:擴展了平移手勢以支持滑動:

 // in handlePan()
switch recognizer.state {


 struct Holder {
                static var lastTranslate : CGFloat = 0
                static var prevTranslate : CGFloat = 0
                static var lastTime : TimeInterval = 0
                static var prevTime : TimeInterval = 0
            }

        case .began:

            Holder.lastTime = Date.timeIntervalSinceReferenceDate
            Holder.lastTranslate = translation.y
            Holder.prevTime = Holder.lastTime
            Holder.prevTranslate = Holder.lastTranslate


            //perform appropriate pan action

        case .changed:

            Holder.prevTime = Holder.lastTime
            Holder.prevTranslate = Holder.lastTranslate
            Holder.lastTime = Date.timeIntervalSinceReferenceDate
            Holder.lastTranslate = translation.y

            //perform appropriate pan action

        case .ended ,.cancelled:

            let seconds = CGFloat(Date.timeIntervalSinceReferenceDate) - CGFloat(Holder.prevTime)
            var swipeVelocity : CGFloat = 0
            if seconds > 0 {
                swipeVelocity = (translation.y - Holder.prevTranslate)/seconds
            }

            var shouldSwipe : Bool = false
            if Swift.abs(swipeVelocity) > velocityThreshold {
                shouldSwipe = swipeVelocity < 0
            }
            if shouldSwipe {
               // perform swipe action
            } else {
                // perform appropriate pan action
            }

        default:
            print("Unsupported")
        }

您所需要做的就是為您的滑動手勢找到合適的velocityTreshold

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM