我有UITabBarController和2 ViewController加在上面。所以现在当我们点击perticular选项卡时,所有选项卡都正确地切换。我将UISwipeGestureRecognizer添加到TabBarController中,在从左向右或从右向左滑动TabBar时具有相同的功能。
点击这里获取图像
但当我试着从右向左或从左到右滑动时,这并不是我的手势
这是我的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发布于 2016-04-29 21:51:38
解决了:它没有检测到滑动的原因是因为它必须是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"];
}https://stackoverflow.com/questions/36943432
复制相似问题