[英]UITabBarController With UISwipeGestureRecognizer
我在UITabBarController上添加了2個ViewController。 因此,當我們單擊“垂直”選項卡時,所有選項卡都已正確切換。 我已將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
已解決:::無法檢測到滑動的原因是因為它必須是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"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.