RDo*_*wns 3 xcode uigesturerecognizer ios swift
我希望能够在我的右侧滑动,ViewController这将显示另一个视图控制器,CommunitiesViewController.
我已经查看了其他线程并找到了一些方法,但我相信它们适用于Swift 2.
这是我在我的代码中使用的代码ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: Selector(("respondToSwipeGesture")))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
print ("Swiped right")
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
//change view controllers
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewController(withIdentifier: "CommunitiesID") as! CommunitiesViewController
self.present(resultViewController, animated:true, completion:nil)
default:
break
}
}
}
Run Code Online (Sandbox Code Playgroud)
我给CommunitiesViewController了一个故事板ID CommunitiesID.
但这不起作用,当我向右滑动时出现以下错误,应用程序崩溃:
libc ++ abi.dylib:以NSException类型的未捕获异常终止
试试:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Gesture Recognizer
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
}
Run Code Online (Sandbox Code Playgroud)
然后添加功能:
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
//right view controller
let newViewController = firstViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
case UISwipeGestureRecognizerDirection.left:
//left view controller
let newViewController = secondViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
default:
break
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5008 次 |
| 最近记录: |