Modify tab in the tab bar controller with your finger
I have three tabs in my pill controller and I want to switch between these tabs in the same way tinder switches tabs with my finger. I did it with help UISwipeGestureRecognizer
, but its not exactly the same as Tinder (dating app). I added UISwipeGestureRecognizer
to one of Tabbar controller
and then added a function to change the selected index in the tab. But the animation is not finger controlled. I want the scrolling to be controlled by my finger.
source to share
I was a little late, but I found my ans -
I created 4 UIviewcontrollers programmatically and then created an array of it.
var views = [CareTeamTableViewController(),VFCChatQViewController(), NewAccountViewController(), ShareViewController()]
Then I added scrolling in my main UiViewController
private func initMainScroll() {
scrollView = UIScrollView.init()
scrollView?.delegate = self
scrollView?.showsHorizontalScrollIndicator = false
scrollView?.isPagingEnabled = true
self.view.addSubview(scrollView!)
}
and then added an array of views like:
func setupScrollView(complete:()->()) {
scrollView?.frame = views.first!.view.frame
scrollView?.contentSize = CGSize(width: CGFloat(views.count) * UIScreen.main.bounds.width, height: 0)
_ = views.map({ addViewToScrollView($0) })
_ = views.map({ $0.view.frame.origin = CGPoint(x: CGFloat(views.index(of: $0)!) * UIScreen.main.bounds.width, y: 0) })
complete()
}
func addViewToScrollView(_ viewController: UIViewController) {
scrollView?.addSubview(viewController.view)
viewController.willMove(toParentViewController: self)
addChildViewController(viewController)
}
source to share
I think the best way to do this is to put all your kinds of tabs in UIScrollView
. You place them next to each other.
Implement scrolling delegate methods in tabbarController
. You will probably need scrollViewDidEndScrollingAnimation
and scrollViewDidEndDecelerating
to find out which screen you are on when the user stops scrolling, for example:
let page_width=UIScreen.main.bounds.width
let page=Int(floor((scrollView.contentOffset.x-page_width/2)/page_width)+1)
Here I am assuming that each kind of your tab stop is the same size as the screen.
source to share