我正在创建swift应用程序,并且我正在使用UISwipegesture我正在向上和向下滑动方向,并且其工作正常,但是当usewr向上或向下滑动时,我将隐藏显示视图,并且按预期方式隐藏并显示,但是当滑动停止时,我想要查看自动显示
让我展示我的代码以更好地理解
代码
videDidLoad()
let swipe = UISwipeGestureRecognizer(target: self, action:
#selector(respondToSwipeGesture(gesture:)))
swipe.direction = UISwipeGestureRecognizer.Direction.up
swipe.delegate = self
self.view.addGestureRecognizer(swipe)
let swipe1 = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
swipe1.direction = UISwipeGestureRecognizer.Direction.down
swipe1.delegate = self
self.view.addGestureRecognizer(swipe1)
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
viewFilter.isHidden = true
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
viewFilter.isHidden = true
default:
break
}
}
}
在这里您可以看到上下方向我隐藏了视图,但是在滑动停止时我想再次显示该视图,这样我就不能站立该怎么做可以帮到我
答案 0 :(得分:3)
使用UIGestureRecognizer.State
appledoc
在选择器中执行以下操作
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
viewFilter.isHidden = true
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
viewFilter.isHidden = true
default:
break
}
// code for looking up which state the gesture currently is in.
switch swipeGesture.state {
case .ended, .failed:
viewFilter.isHidden = false
// list up other cases here
}
}
}
答案 1 :(得分:0)
您可以将state
用于手势识别器:
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizer.Direction.up:
print("Swiped up")
viewFilter.isHidden = true
case UISwipeGestureRecognizer.Direction.down:
print("Swiped down")
viewFilter.isHidden = true
default:
break
}
if swipeGesture.state == .ended {
viewFilter.isHidden = false
}
}
}
答案 2 :(得分:0)
您可以在下面进行操作,获得UIGestureRecognizer
中的ended state。看到这个doc
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
if swipeGesture.state == .ended {
viewFilter.isHidden = false
}
}
}