我正在尝试将方法注册为视图控制器中 UISwipeGestureRecognizer
的操作 . 目标视图是我使用 UIView
子类构建的自定义视图,因为's where I want the swipe targeted at. The problem I' m具有的是我不知道如何将操作设置为 UIViewController
中的方法 . 当我将手势拖放到界面构建器中的视图时,我可以执行此操作,然后将操作拖出到控制器 .
func swiped(_ sender: UISwipeGestureRecognizer) {
}
func addGestureToView(_ view: CardView) {
let gesture = UISwipeGestureRecognizer(target: view, action: Selector("swiped:"))
gesture.direction = .up
view.addGestureRecognizer(gesture)
}
我也试过用
let gesture = UISwipeGestureRecognizer(target: view, action: #selector(swiped(_:)))
这两种方法都给了我以下例外:
由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [testapp.CardView swiped:]:发送到实例的无法识别的选择器
我假设这是因为它在 CardView
目标中寻找 swiped:
,而不是在Controller中存在的方法 . 我如何指定我希望它将Controller用于操作,同时仅将手势本身应用于 CardView
?
1 回答
改变这一行:
至:
Explanation
target: 在UISwipeGestureRecognizer中定义了你的动作方法的调用位置,通过定义self我们告诉我们应该从UIViewcontroller而不是从CardView调用我们的滑动手势动作
UISwipeGestureRecognizer中的 action: 定义了应该在滑动手势上调用的方法 .