我想使用滑动手势在我的标签栏之间导航。最简单的方法是什么?我试过这样的事......
import UIKit
class postAdViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
view.addGestureRecognizer(leftSwipe)
}
func handleSwipes(sender:UISwipeGestureRecognizer) {
if (sender.direction == .left) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "favourireviewcontroller") as! UIViewController
self.present(vc, animated: true, completion: nil)
}
if (sender.direction == .right) {
}
}
如果我试图向右滑动没有任何反应。滑动时,应用程序崩溃时出现以下错误消息
无法识别的选择器发送到实例0x7f924380a730
答案 0 :(得分:3)
如果您要浏览tabBar
,那么您应该为swipeGestureRecognizer
和.left
实施.right
,然后使用tabBarController?.selectedIndex
,例如这样:
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2 { // set your total tabs here
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
答案 1 :(得分:3)
这是一个通过TabBar控制器左右滑动的Swift4示例。
我不喜欢这个解决方案: - 似乎你应该能够注册一次处理程序并区分处理程序本身的方向,但我无法轻易做到这一点。 - 我也很好奇如何做一个包含拖动视觉效果的手势。我认为我还需要包含一个PanGesture才能实现这一目标。
SELECT item.* FROM
item INNER JOIN categories ON item.category_id = categories.id
WHERE item.is_active = 1 AND (categories.is_active = 1 AND categories.parent_id IN
(SELECT id FROM categories WHERE parent_id IS NULL AND is_active = 1)) OR ( item.is_active = 1 AND categories.parent_id IS NULL AND categories.is_active = 1 )
然后是处理程序:
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}
答案 2 :(得分:1)
尝试使用Swift 4选择器语法:
//below code write in view did appear()
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swiped))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
// below code create swipe gestures function
// MARK: - swiped
@objc func swiped(_ gesture: UISwipeGestureRecognizer) {
if gesture.direction == .left {
if (self.tabBarController?.selectedIndex)! < 2
{ // set here your total tabs
self.tabBarController?.selectedIndex += 1
}
} else if gesture.direction == .right {
if (self.tabBarController?.selectedIndex)! > 0 {
self.tabBarController?.selectedIndex -= 1
}
}
}
答案 3 :(得分:0)
尝试使用Swift 3
选择器语法:
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))
喜欢这样
override func viewDidLoad() {
super.viewDidLoad()
nextButton.layer.cornerRadius = 7
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(_:))
//leftSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
}
func handleSwipes(_ sender: UISwipeGestureRecognizer) {
if (sender.direction == .left) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "favourireviewcontroller") as! UIViewController
self.present(vc, animated: true, completion: nil)
}
if (sender.direction == .right) {
}
}
Swift 3
引入了此功能,使编译器能够检查您指定的function
是否确实存在。因此,它比以前的概念更安全。
答案 4 :(得分:0)
这是先前建议的稍作修改的版本,可用于 Swift 5 和iOS 12。
真正的优势是:
guard
语句,因此您不必弄乱在处理程序中解开tabBarController
和viewControllers
viewControllers
数组中获取它即可。我也对其进行了一些清理,以使其不再那么冗长。它已经在iOS 12上运行的Swift 5进行了测试(在iOS 11.4上应该没问题,因为这是我测试过的最低部署版本)。
将它们添加到viewDidLoad
(或您选择的其他适当位置):
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGesture))
swipeRight.direction = .right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGesture))
swipeLeft.direction = .left
self.view.addGestureRecognizer(swipeLeft)
然后将其添加为事件的处理程序:
@objc func handleSwipeGesture(_ gesture: UISwipeGestureRecognizer) {
guard let tabBarController = tabBarController, let viewControllers = tabBarController.viewControllers else { return }
let tabs = viewControllers.count
if gesture.direction == .left {
if (tabBarController.selectedIndex) < tabs {
tabBarController.selectedIndex += 1
}
} else if gesture.direction == .right {
if (tabBarController.selectedIndex) > 0 {
tabBarController.selectedIndex -= 1
}
}
}