Give pierwszeństwo przesunięcia
Możesz podać priorytet do UIGestureRecognizer
za pomocą metody require(toFail:)
.
@IBOutlet var myPanGestureRecognizer: UIPanGestureRecognizer!
@IBOutlet var mySwipeGestureRecognizer: UISwipeGestureRecognizer!
myPanGesture.require(toFail: mySwipeGestureRecognizer)
teraz Twój pan będzie wykonać tylko wtedy, gdy machnięcia zawiedzie.
Zastosowanie pan za wszystko
Jeśli machnięcia i pan gest aparatów rozpoznawania nie grają ładnie z tej konfiguracji można toczyć całą swoją logikę do patelni Rozpoznawanie gestów dla większej kontroli.
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
}
}
Oto demo powyższego kodu w akcji.
Używam "https://github.com/XavierDK/XDKAirMenu", a także używam gestu przeciągnięcia w jednym z kontrolerów, więc ten uczestnik nie działa dla mnie – iAnkit