2016-04-29 68 views
0

我有UITabBarController与2 ViewController添加它。所以,现在当我们点击特定标签时,所有标签切换正常。我在TabBarController中添加了UISwipeGestureRecognizer,它可以将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"]; 
} 
相关问题