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 swipe và chả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.

Nguồn
2018-01-13 00:27:49
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