我有 UIViewController 并在故事板中添加了 UITableView,后来我向视图添加了向上滑动手势识别器,但什么也没发生。
这是我的代码
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let swipeRegongnizer = UISwipeGestureRecognizer(target: self, action: #selector(self.handleSwipeUp))
swipeRegongnizer.direction = UISwipeGestureRecognizerDirection.up
swipeRegongnizer.delegate = self
tableview.addGestureRecognizer(swipeRegongnizer)
}
func handleSwipeUp(gesture: UISwipeGestureRecognizer) {
print("swiped up")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "cell\(indexPath.row)"
return cell
}
}
1.实现 UIGestureRecognizerDelegate。2.设置委托。
yourGesture.delegate = self
3.在您各自的 swift 文件中添加以下函数。
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)