I'm currently running an app that shows a UITableViewController
, with data dynamically loaded into it. On the navigation bar, i have a "previous" and "next" button, that loads new data in the current view controller. It works really well.
我目前正在运行一个应用程序,它显示一个UITableViewController,其中包含动态加载的数据。在导航条上,我有一个“先前”和“下一步”按钮,它在当前视图控制器中加载新数据。它工作得很好。
What i want i taking it to the next level : i'd like to be able to detect left and right swipe gestures in order to show animated transitions with next / previous pages. For instance, when i swipe right i'd like to have a transition showing a new UITableViewController
, with the correct data that would be loaded in the background. Something like a PageViewController
, but with infinite number of pages.
我想让它进入下一个层次:我想要能够检测左右滑动手势,以显示下一页/前一页的动画转换。例如,当我向右滑动时,我想要有一个显示一个新的UITableViewController的过渡,这个UITableViewController有正确的数据会被加载到后台。有点像PageViewController,但是有无数的页面。
Is there a trivial way to do this? Or do i have to go with GesturesDetecror
and custom transitions ?
有什么简单的方法吗?还是必须使用GesturesDetecror和自定义转换?
Thanks a lot !
谢谢!
1 个解决方案
#1
1
Gesture recognizers are implemented for this purpose. There is a class specifically for swiping left and swiping right, which is what I think you're looking for.
为此目的实现了手势识别器。有一个专门的类用于向左和向右滑动,我认为这就是你要找的。
If I understand correctly, you're wanting to implement swipes in order to replace what the current next and previous buttons are doing, plus some swiping animation to transition between tables of data. The swipe detection can be accomplished by simply adding the appropriate gesture recognizers to the table view. The appropriate gesture recognizer here is the UISwipeGestureRecognizer class.
如果我理解正确,您希望实现swipes,以替换当前和前面的按钮正在执行的操作,再加上一些swiping动画,以便在数据表之间转换。只需在表视图中添加适当的手势识别器,就可以实现滑动检测。这里适当的手势识别器是UISwipeGestureRecognizer类。
This can be done programmatically (probably in the viewDidLoad of the VC). First, create a UISwipeGestureRecognizer:
这可以通过编程完成(可能在VC的viewDidLoad中)。首先,创建一个UISwipeGestureRecognizer:
var swipeRight: UIGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "didSwipeRight")
Default direction for a gesture recognizer is right, so theres no need to set the swipes direction. The direction is stored in the gesture recognizers direction property. Now add the gesture recognizer to the tableview:
手势识别器的默认方向是正确的,所以不需要设置滑动方向。方向存储在手势识别器方向属性中。现在将手势识别器添加到tableview:
tableView.addGestureRecognizer(swipeRight)
After this setup these two lines of code the view controller is set up to call the function didSwipeRight any time a swipe right gesture is detected.
设置完这两行代码后,视图控制器将被设置为在检测到右击手势时调用函数didSwipeRight。
func didSwipeRight() {
// Code to go to previous page
}
Then do the same for the swipe left, only you have to include a line to set the direction to be left.
然后对向左滑动做同样的操作,只需要包含一条线来设置要向左的方向。
var swipeLeft: UIGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "didSwipeLeft")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
tableView.addGestureRecognizer(swipeLeft)
And then the function to handle the left swipes...
然后处理左勾选的函数。
func didSwipeLeft() {
//Code to go to next page.
}
And then implement the transitions within the body of their corresponding functions. Since you are wanting an infinite number of pages, I think the solution is to create a single view controller configured to show a transition with every swipe, but in reality it is only a single view controller. Hope that helps.
然后在相应函数的主体内实现转换。因为您需要无限的页面,所以我认为解决方案是创建一个视图控制器,配置为每次滑动都显示一个转换,但实际上它只是一个视图控制器。希望有帮助。
#1
1
Gesture recognizers are implemented for this purpose. There is a class specifically for swiping left and swiping right, which is what I think you're looking for.
为此目的实现了手势识别器。有一个专门的类用于向左和向右滑动,我认为这就是你要找的。
If I understand correctly, you're wanting to implement swipes in order to replace what the current next and previous buttons are doing, plus some swiping animation to transition between tables of data. The swipe detection can be accomplished by simply adding the appropriate gesture recognizers to the table view. The appropriate gesture recognizer here is the UISwipeGestureRecognizer class.
如果我理解正确,您希望实现swipes,以替换当前和前面的按钮正在执行的操作,再加上一些swiping动画,以便在数据表之间转换。只需在表视图中添加适当的手势识别器,就可以实现滑动检测。这里适当的手势识别器是UISwipeGestureRecognizer类。
This can be done programmatically (probably in the viewDidLoad of the VC). First, create a UISwipeGestureRecognizer:
这可以通过编程完成(可能在VC的viewDidLoad中)。首先,创建一个UISwipeGestureRecognizer:
var swipeRight: UIGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "didSwipeRight")
Default direction for a gesture recognizer is right, so theres no need to set the swipes direction. The direction is stored in the gesture recognizers direction property. Now add the gesture recognizer to the tableview:
手势识别器的默认方向是正确的,所以不需要设置滑动方向。方向存储在手势识别器方向属性中。现在将手势识别器添加到tableview:
tableView.addGestureRecognizer(swipeRight)
After this setup these two lines of code the view controller is set up to call the function didSwipeRight any time a swipe right gesture is detected.
设置完这两行代码后,视图控制器将被设置为在检测到右击手势时调用函数didSwipeRight。
func didSwipeRight() {
// Code to go to previous page
}
Then do the same for the swipe left, only you have to include a line to set the direction to be left.
然后对向左滑动做同样的操作,只需要包含一条线来设置要向左的方向。
var swipeLeft: UIGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "didSwipeLeft")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
tableView.addGestureRecognizer(swipeLeft)
And then the function to handle the left swipes...
然后处理左勾选的函数。
func didSwipeLeft() {
//Code to go to next page.
}
And then implement the transitions within the body of their corresponding functions. Since you are wanting an infinite number of pages, I think the solution is to create a single view controller configured to show a transition with every swipe, but in reality it is only a single view controller. Hope that helps.
然后在相应函数的主体内实现转换。因为您需要无限的页面,所以我认为解决方案是创建一个视图控制器,配置为每次滑动都显示一个转换,但实际上它只是一个视图控制器。希望有帮助。