2012-09-21 7 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]; 
} 
私はそうのようなpageviewcontrollerに全体の事を埋め込むことをお勧めします
+0

元の投稿のAndroidのYouTubeの例のようにアニメーション化することはできませんか? – user1689272

+1

私は過去にもっと流動的なタブコントロールを作成しましたが、これは上記のタブバーの例よりもはるかにカスタムのソリューションでした。前回私がそういったことをしたとき、私は横に並んだセクションビューコントローラーをすべて含んだ、本当に長いビューコントローラーを作成しました(VCが画面幅の5倍になるように5つのセクションがありました)。ナビゲーションは、コンテナVCを左右にスライドさせることを含む。すべての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を使用していることができますと仮定すると、あなたのためにすべての重労働を行うクラスから継承する。

これは私がUITabControllersの一部であるすべてのあなたのviewcontrollersがSwipableTabVCの代わりのUIViewControllerを継承することができます

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) 
    } 
} 

をそれを行っている方法です。

関連する問題