2016-04-29 70 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"]; 
} 
相關問題