首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITabBarController与UISwipeGestureRecognizer

UITabBarController与UISwipeGestureRecognizer
EN

Stack Overflow用户
提问于 2016-04-29 16:35:01
回答 1查看 345关注 0票数 0

我有UITabBarController和2 ViewController加在上面。所以现在当我们点击perticular选项卡时,所有选项卡都正确地切换。我将UISwipeGestureRecognizer添加到TabBarController中,在从左向右或从右向左滑动TabBar时具有相同的功能。

点击这里获取图像

但当我试着从右向左或从左到右滑动时,这并不是我的手势

这是我的TabBarController代码

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-29 21:51:38

解决了:它没有检测到滑动的原因是因为它必须是IBAction。

代码语言:javascript
复制
-(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"];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36943432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档