2012-09-21 30 views

回答

1

當然,這是可能的。

每個屏幕都需要有一個UISwipeGestureRecognizer用於滑動,然後調用標籤欄執行所需的操作。這可能是任何東西從增加或減少活動選項卡到任何你想要的。

爲了防止代碼重複,您可以創建一個自定義的UIViewController並讓所有視圖控制器從那裏繼承(或其他方式)。

20

如果您使用的是標籤欄控制器,您可以在每個標籤的視圖上設置一個輕掃手勢識別器。當手勢識別器被觸發時,它可以改變tabBarController.selectedTabIndex

這個效果不會動畫化,但它會用滑動手勢切換選項卡。這大概就是我使用左右側帶有按鈕的UITabBar的應用程序,然後滑動手勢以更改活動選項卡。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRightButton:)]; 
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.view addGestureRecognizer:swipeLeft]; 

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton:)]; 
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [self.view addGestureRecognizer:swipeRight]; 
} 

- (IBAction)tappedRightButton:(id)sender 
{ 
    NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex]; 

    [rootVC.tabBarController setSelectedIndex:selectedIndex + 1]; 
} 

- (IBAction)tappedLeftButton:(id)sender 
{ 
    NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex]; 

    [rootVC.tabBarController setSelectedIndex:selectedIndex - 1]; 
} 
+0

因此,在原始帖子中的Android youtube示例中無法創建動畫效果? – user1689272

+1

我在過去創建了一個更流暢的選項卡控件,它比上面的選項卡示例更爲自定義的解決方案。最後一次我做了這樣的事情,我創建了一個非常長的視圖控制器,它包含並排佈置的所有剖面視圖控制器(我有5個剖面,所以VC是屏幕寬度的5倍)。導航涉及將容器VC左右滑動。這並不是最好的方法,因爲所有風險投資人始終都在記憶中,但是這是我所需要的最好的方式。不幸的是,我無法顯示任何代碼片段。 – skladek

+3

從iOS7開始,可以使用具有自定義視圖控制器轉換的'UIPercentDrivenInteractiveTransition'來實現此效果。基本上你會有一個導航控制器與自定義推/流行動畫做滑動。 UIPercentDrivenInteractiveTransition將允許轉換作爲一個平移而不是一個滑動。 – skladek

6

試試這個,

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRightButton:)]; 
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 
    [self.view addGestureRecognizer:swipeLeft]; 

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton:)]; 
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; 
    [self.view addGestureRecognizer:swipeRight]; 
} 

- (IBAction)tappedRightButton:(id)sender 
{ 
    NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex]; 

    [self.tabBarController setSelectedIndex:selectedIndex + 1]; 

    //To animate use this code 
    CATransition *anim= [CATransition animation]; 
    [anim setType:kCATransitionPush]; 
    [anim setSubtype:kCATransitionFromRight]; 
    [anim setDuration:1]; 
    [anim setTimingFunction:[CAMediaTimingFunction functionWithName: 
            kCAMediaTimingFunctionEaseIn]]; 
    [self.tabBarController.view.layer addAnimation:anim forKey:@"fadeTransition"]; 
} 

- (IBAction)tappedLeftButton:(id)sender 
{ 
    NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex]; 

    [self.tabBarController setSelectedIndex:selectedIndex - 1]; 

    CATransition *anim= [CATransition animation]; 
    [anim setType:kCATransitionPush]; 
    [anim setSubtype:kCATransitionFromRight]; 

    [anim setDuration:1]; 
    [anim setTimingFunction:[CAMediaTimingFunction functionWithName: 
           kCAMediaTimingFunctionEaseIn]]; 
    [self.tabBarController.view.layer addAnimation:anim forKey:@"fadeTransition"]; 
} 
+0

謝謝,它確實有幫助。 –

2

假設你正在使用UITabBarConroller

所有孩子ViewControllers可以繼承一個爲你完成所有繁重工作的課程。

這是我如何做它

class SwipableTabVC : UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft)) 
     left.direction = .left 
     self.view.addGestureRecognizer(left) 

     let right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight)) 
     right.direction = .right 
     self.view.addGestureRecognizer(right) 
    } 

    func swipeLeft() { 
     let total = self.tabBarController!.viewControllers!.count - 1 
     tabBarController!.selectedIndex = min(total, tabBarController!.selectedIndex + 1) 

    } 

    func swipeRight() { 
     tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1) 
    } 
} 

所以所有的viewcontrollers是UITabControllers的部分可以從SwipableTabVC的UIViewController,而不是繼承。