Change tab in tab bar controller using finger swipe

浪子不回头ぞ 提交于 2021-02-07 20:54:21

问题


I have three tabs in my tabbar controller and I want to switch between these tabs just like tinder switches the tab using finger touch. I have done it using UISwipeGestureRecognizer but its not exactly same as that of Tinder (the dating app ) swiping. I have added UISwipeGestureRecognizer on one of the Tabbar controller and then added the function to change the tabbar selected index. But the animations is not controlled by finger touch. I want the swiping should be controlled by finger touch.


回答1:


I think the best way to do it is to put all your tab views in a UIScrollView. You place them next to each other.

Implement the scroll view delegate methods in your tabbarController. You'll probably need scrollViewDidEndScrollingAnimation and scrollViewDidEndDecelerating to know on which view you are when the user stops scrolling, like this:

let page_width=UIScreen.main.bounds.width
let page=Int(floor((scrollView.contentOffset.x-page_width/2)/page_width)+1)

Here, I assume each of your tab view is the same size as the screen.




回答2:


I am bit late but I found my ans -

I have created 4 UIviewcontrollers programatically and then created an array of it.

var views = [CareTeamTableViewController(),VFCChatQViewController(), NewAccountViewController(), ShareViewController()]

Then I added a scrollview 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 the views array 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)
    }


来源:https://stackoverflow.com/questions/43783717/change-tab-in-tab-bar-controller-using-finger-swipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!