25

UISwipeGestureRecognizerUIPanGestureRecognizerを同時に動作させることができるように、ジェスチャレコグナイザーをどのように設定しますか?タッチしてすばやく移動すると(クイックスワイプ)、ジェスチャはスワイプとして検出されますが、タッチしてから移動すると(タッチと移動の間の短い遅延)、パンとして検出されますか?

requireGestureRecognizerToFailのさまざまな順列を試しましたが、正確には役に立ちませんでした。SwipeGestureを左にすると、パンジェスチャは上下左右に機能しますが、左の動きはスワイプジェスチャによって検出されます。

4

4 に答える 4

52

2つUIGestureRecognizerのデリゲートの1つを、意味のある(おそらく)オブジェクトに設定してからリッスンし、このメソッドselfに戻ります。YES

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
         shouldRecognizeSimultaneouslyWithGestureRecognizer:
                            (UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

このメソッドは、いずれgestureRecognizerかによるジェスチャの認識、またはotherGestureRecognizer他のジェスチャ認識機能によるジェスチャの認識をブロックする場合に呼び出されます。返品YESは同時認識を可能にすることが保証されていることに注意してください。一方、戻るNOと、他のジェスチャ認識機能の代理人が戻る可能性があるため、同時認識を防ぐことは保証されませんYES

于 2011-02-25T03:10:18.267 に答える
8

デフォルトでは、ユーザーがスワイプしようとすると、ジェスチャはパンとして解釈されます。これは、スワイプジェスチャが、スワイプ(個別ジェスチャ)として解釈されるために必要な条件を満たす前に、パン(連続ジェスチャ)として解釈されるために必要な条件を満たしているためです。

遅延させたいジェスチャレコグナイザーのrequireGestureRecognizerToFail:メソッドを呼び出して、2つのジェスチャレコグナイザー間の関係を示す必要があります。

[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer];
于 2015-08-05T21:00:59.860 に答える
6

パンレコグナイザーを使用してスワイプとパンを検出する

- (void)setupRecognizer
{
    UIPanGestureRecognizer* panSwipeRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanSwipe:)];
    // Here you can customize for example the minimum and maximum number of fingers required
    panSwipeRecognizer.minimumNumberOfTouches = 2;
    [targetView addGestureRecognizer:panSwipeRecognizer];
}

#define SWIPE_UP_THRESHOLD -1000.0f
#define SWIPE_DOWN_THRESHOLD 1000.0f
#define SWIPE_LEFT_THRESHOLD -1000.0f
#define SWIPE_RIGHT_THRESHOLD 1000.0f

- (void)handlePanSwipe:(UIPanGestureRecognizer*)recognizer
{
    // Get the translation in the view
    CGPoint t = [recognizer translationInView:recognizer.view];
    [recognizer setTranslation:CGPointZero inView:recognizer.view];

    // TODO: Here, you should translate your target view using this translation
    someView.center = CGPointMake(someView.center.x + t.x, someView.center.y + t.y);

    // But also, detect the swipe gesture
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        CGPoint vel = [recognizer velocityInView:recognizer.view];

        if (vel.x < SWIPE_LEFT_THRESHOLD)
        {
            // TODO: Detected a swipe to the left
        }
        else if (vel.x > SWIPE_RIGHT_THRESHOLD)
        {
            // TODO: Detected a swipe to the right
        }
        else if (vel.y < SWIPE_UP_THRESHOLD)
        {
            // TODO: Detected a swipe up
        }
        else if (vel.y > SWIPE_DOWN_THRESHOLD)
        {
            // TODO: Detected a swipe down
        }
        else
        {
            // TODO:
            // Here, the user lifted the finger/fingers but didn't swipe.
            // If you need you can implement a snapping behaviour, where based on the location of your         targetView,
            // you focus back on the targetView or on some next view.
            // It's your call
        }
    }
}
于 2014-11-07T16:43:02.620 に答える
2

パンとスワイプの方向を検出するための完全なソリューションは次のとおりです(2cupsOfTechのswipeThresholdロジックを利用):

public enum PanSwipeDirection: Int {
    case up, down, left, right, upSwipe, downSwipe, leftSwipe, rightSwipe
    public var isSwipe: Bool { return [.upSwipe, .downSwipe, .leftSwipe, .rightSwipe].contains(self) }
    public var isVertical: Bool { return [.up, .down, .upSwipe, .downSwipe].contains(self) }
    public var isHorizontal: Bool { return !isVertical }
}

public extension UIPanGestureRecognizer {

   var direction: PanSwipeDirection? {
        let SwipeThreshold: CGFloat = 1000
        let velocity = self.velocity(in: view)
        let isVertical = abs(velocity.y) > abs(velocity.x)
        switch (isVertical, velocity.x, velocity.y) {
        case (true, _, let y) where y < 0: return y < -SwipeThreshold ? .upSwipe : .up
        case (true, _, let y) where y > 0: return y > SwipeThreshold ? .downSwipe : .down
        case (false, let x, _) where x > 0: return x > SwipeThreshold ? .rightSwipe : .right
        case (false, let x, _) where x < 0: return x < -SwipeThreshold ? .leftSwipe : .left
        default: return nil
        }
    }

}

使用法:

@IBAction func handlePanOrSwipe(recognizer: UIPanGestureRecognizer) {

    if let direction = recognizer.direction {
        if direction == .leftSwipe {
            //swiped left
        } else if direction == .up {
            //panned up
        } else if direction.isVertical && direction.isSwipe {
            //swiped vertically
        }
    }

}
于 2017-04-13T15:28:31.207 に答える