0
votes

I have UITabBarController with 2 ViewController added on it. So Now all tabs switched properly when we click on perticular tab. I have added UISwipeGestureRecognizer to TabBarController the same functionality on swiping the TabBar from left to right or righ to left.

Click here for Image

But I when I try to swipe from right to left or from left to right, it's NOT detecting my gesture

Here's my Code for 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
1
Swiping to move between tabs is not a very common activity and isn't very intuitive. I would recommend you reconsider doing this. When you swipe, are your "...SwipeDidFire" methods being called at all? - fsb
Solved,, The reason why it doesn't detect the swipe is because it's has to be IBAction @fbara - Assam AlZookery

1 Answers

0
votes

SOLVED::: The reason why it doesn't detect the swipe is because it's has to be 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"];
}