2016-04-29 12 views
0

私は2 ViewControllerを追加したUITabBarControllerを持っています。これで、perticularタブをクリックすると、すべてのタブが正しく切り替わりました。 UISwipeGestureRecognizerをTabBarControllerに追加したのは、TabBarを左から右に、または左から左にスワイプするときと同じ機能です。UITabBarController UISwipeGestureRecognizerで

Click here for Image

しかし、私は左または右に左から右からスワイプしようとしたとき、私は、それは私のジェスチャー

を検出していないがここで解決しようTabBarController

#import "TabBarController.h" 

@implementation TabBarController 
-(void)viewDidLoad{ 

    UISwipeGestureRecognizer *leftToRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftToRightSwipeDidFire)]; 
    leftToRightGesture.direction = UISwipeGestureRecognizerDirectionRight; 
    [self.tabBarController.tabBar addGestureRecognizer:leftToRightGesture]; 

    UISwipeGestureRecognizer *rightToLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightToLeftSwipeDidFire)]; 
    rightToLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft; 
    [self.tabBarController.tabBar addGestureRecognizer:rightToLeftGesture]; 

} 

- (void)leftToRightSwipeDidFire { 
    UITabBar *tabBar = self.tabBarController.tabBar; 
    NSInteger index = [tabBar.items indexOfObject:tabBar.selectedItem]; 
    if (index > 0) { 
     self.tabBarController.selectedIndex = index - 1; 
    } else { 
     return; 
    } 
} 
- (void)rightToLeftSwipeDidFire { 
    UITabBar *tabBar = self.tabBarController.tabBar; 
    NSInteger index = [tabBar.items indexOfObject:tabBar.selectedItem]; 
    if (index < tabBar.items.count - 1) { 
     self.tabBarController.selectedIndex = index + 1; 
    } else { 
     return; 
    } 
} 

@end 
+0

タブを移動するためにスワイプすることはあまり一般的ではなく、あまり直感的ではありません。私はこれを再考することをお勧めします。あなたがスワイプすると、あなたの "... SwipeDidFire"メソッドが全く呼び出されていますか? – fsb

+0

解決済み、スワイプを検出しない理由は、IBAction @fbara –

答えて

0

のための私のコードです: :: スワイプを検出しない理由は、IBActionでなければならないためです。

-(void)viewDidLoad{ 
    [super viewDidLoad]; 



    NSString *ipAddressText = @"192.168.211.62"; 
    NSString *portText = @"12"; 

    NSLog(@"Setting up connection to %@ : %i", ipAddressText, [portText intValue]); 
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) ipAddressText, [portText intValue], &readStream, &writeStream); 

    messages = [[NSMutableArray alloc] init]; 
    [self open]; 

    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]; 


    // [delegate TCPSOCKET]; 
    //ViewSwipe *theInstance = [[ViewSwipe alloc]init]; 
    //[theInstance TCPSOCKET]; 
} 

- (IBAction)tappedRightButton:(id)sender 
{ 
    NSUInteger selectedIndex = [self.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 = [self.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"]; 
} 
関連する問題