9

Tôi đang phát triển một ứng dụng mà tôi đã sử dụng Pan Gesture cũng như Swipe Gesture. Vì vậy, mỗi khi tôi làm cử chỉ swipe nhưng phương pháp từ cử chỉ Pan luôn luôn nhận được gọi là và swipe cử chỉ phương pháp không nhận được gọi.Có bất kỳ điều kiện ưu tiên nào giữa các phương thức cử chỉ hay không (Pan Gesture và Swipe Gesture)?

Có bất kỳ mức độ ưu tiên nào giữa tất cả phương pháp cử chỉ không?

Trả lời

11

Bạn có thể gọi cho họ song song bằng cách thực hiện các phương pháp sau đây của UIGestureRecognizerDelegate giao thức:

- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer 
{ 
    return YES; 
} 
+0

Tôi đang sử dụng "https://github.com/XavierDK/XDKAirMenu" và cũng đang sử dụng cử chỉ vuốt trong một bộ điều khiển của tôi để đại biểu này không hoạt động đối với tôi – iAnkit

9

Có thuộc tính trên lớp UIGestureRecognizer được gọi là "cancelsTouchesInView" mặc định là YES. Điều này sẽ khiến mọi cử chỉ đang chờ xử lý bị hủy. Cử chỉ Pan được nhận diện đầu tiên vì nó không cần phải có sự kiện "chạm", vì vậy nó sẽ hủy cử chỉ vuốt.

Nếu bạn muốn cả hai cử chỉ để được công nhận, hãy thử thêm:

[yourPanGestureInstance setCancelsTouchesInView:NO];

+0

Cảm ơn bạn đã trả lời Nhưng phương pháp này vẫn không Xác định Swipe Gesture – NIKHIL

+1

tôi đã cũng cố gắng shouldRecognizeSimultaneouslyWithGestureRecognizer Phương pháp Trong UIGestureRecognizer.H nộp – NIKHIL

+1

bạn đã thử sử dụng requireGestureRecognizerToFail :? –

0

Hãy cho mức độ ưu tiên để vuốt

Bạn có thể cung cấp ưu tiên cho một số UIGestureRecognizer với phương thức require(toFail:).

@IBOutlet var myPanGestureRecognizer: UIPanGestureRecognizer! 
@IBOutlet var mySwipeGestureRecognizer: UISwipeGestureRecognizer! 

myPanGesture.require(toFail: mySwipeGestureRecognizer) 

Bây giờ bạn chảo sẽ chỉ thực hiện nếu bạn swipe thất bại.


Sử dụng chảo cho tất cả mọi thứ

Nếu swipechảo recognizers cử chỉ không chơi độc đáo với thiết lập này, bạn có thể cuộn tất cả các logic của bạn vào chảo trình nhận dạng cử chỉ để kiểm soát nhiều hơn.

let minHeight: CGFloat = 100 
let maxHeight: CGFloat = 700 
let swipeVelocity: CGFloat = 500 
var previousTranslationY: CGFloat = 0 

@IBOutlet weak var cardHeightConstraint: NSLayoutConstraint! 

@IBAction func didPanOnCard(_ sender: Any) { 

    guard let panGesture = sender as? UIPanGestureRecognizer else { return } 

    let gestureEnded = bool(panGesture.state == UIGestureRecognizerState.ended) 
    let velocity = panGesture.velocity(in: self.view) 

    if gestureEnded && abs(velocity.y) > swipeVelocity { 
     handlePanOnCardAsSwipe(withVelocity: velocity.y) 
    } else { 
     handlePanOnCard(panGesture) 
    } 
} 

func handlePanOnCard(_ panGesture: UIPanGestureRecognizer) { 

    let translation = panGesture.translation(in: self.view) 
    let translationYDelta = translation.y - previousTranslationY 

    if abs(translationYDelta) < 1 { return } // ignore small changes 

    let newCardHeight = cardHeightConstraint.constant - translationYDelta 

    if newCardHeight > minHeight && newCardHeight < maxHeight { 
     cardHeightConstraint.constant = newCardHeight 
     previousTranslationY = translation.y 
    } 

    if panGesture.state == UIGestureRecognizerState.ended { 
     previousTranslationY = 0 
    } 
} 

func handlePanOnCardAsSwipe(withVelocity velocity: CGFloat) { 
    if velocity.y > 0 { 
     dismissCard() // implementation not shown 
    } else { 
     maximizeCard() // implementation not shown 
    } 
} 

Đây là bản demo của mã ở trên đang hoạt động.

enter image description here

Các vấn đề liên quan