iOS中UITabbarController左右滑动切换;UIViewController左右滑动;滑动切换;UITabbarController左右切换
转载请注明出处:http://blog.csdn.net/infant09/article/details/48773281
在经典的带有tabbar的app中,一般是引入一个UITabbarController,再添加多个UIViewController实现的。一般通过点击不同的UITabbarItem,实现不同的UIViewController切换。目前许多经典的app都是这样切换的,比如微信、微博以及iPhone自带的一些应用等等。
有时候也会有特殊的需求,比如要求左右滑动实现UIViewController的切换(例如android版的微信、QQ就可以这样,当然iOS版不这样,个人觉得可能和左右滑动会误操作有关?)。这时候如何实现呢?
原理很简单,即为每个View添加Pan手势,能够左右滑动。当滑动过半的时候,tabbar切换页面。并且当每个ViewController DidAppear后,将左右两边的Controller渲染出来,添加到当前的View中(成为subView)。
下面给出一个例程。
选择新建一个“Tabbed Application”
做到左右滑动的关键点,即为每个ViewController的self.view增加一个Pan手势。以SecondViewController为例,在viewDidLoad中添加:
//添加左右滑动手势pan
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:pan];
并添加handlePan:方法:
- (void) handlePan:(UIPanGestureRecognizer*)recongizer{
NSLog(@"UIPanGestureRecognizer");
NSUInteger index = [self.tabBarController selectedIndex];
CGPoint point = [recongizer translationInView:self.view];
NSLog(@"%f,%f",point.x,point.y);
recongizer.view.center = CGPointMake(recongizer.view.center.x + point.x, recongizer.view.center.y);
[recongizer setTranslation:CGPointMake(0, 0) inView:self.view];
if (recongizer.state == UIGestureRecognizerStateEnded) {
if (recongizer.view.center.x < [UIScreen mainScreen].bounds.size.width && recongizer.view.center.x > 0 ) {
[UIView animateWithDuration:time delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
recongizer.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2 ,[UIScreen mainScreen].bounds.size.height/2);
}completion:^(BOOL finished) {
}];
} else if (recongizer.view.center.x <= 0 ){
[UIView animateWithDuration:time delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
recongizer.view.center = CGPointMake(-[UIScreen mainScreen].bounds.size.width/2 ,[UIScreen mainScreen].bounds.size.height/2);
}completion:^(BOOL finished) {
[self.tabBarController setSelectedIndex:index+1];
recongizer.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2 ,[UIScreen mainScreen].bounds.size.height/2);
}];
} else {
[UIView animateWithDuration:time delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
recongizer.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width*1.5 ,[UIScreen mainScreen].bounds.size.height/2);
}completion:^(BOOL finished) {
[self.tabBarController setSelectedIndex:index-1];
recongizer.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2 ,[UIScreen mainScreen].bounds.size.height/2);
}];
}
}
}
NSUInteger selectedIndex = [self.tabBarController selectedIndex];
UIViewController* v1 = [self.tabBarController.viewControllers objectAtIndex:selectedIndex-1];
UIImage* image1 = [self imageByCropping:v1.view toRect:v1.view.bounds];
_imageviewLeft = [[UIImageView alloc] initWithImage:image1];
_imageviewLeft.frame = CGRectMake(_imageviewLeft.frame.origin.x - [UIScreen mainScreen].bounds.size.width, _imageviewLeft.frame.origin.y , _imageviewLeft.frame.size.width, _imageviewLeft.frame.size.height);
[self.view addSubview:_imageviewLeft];
UIViewController* v2 = [self.tabBarController.viewControllers objectAtIndex:selectedIndex+1];
UIImage* image2 = [self imageByCropping:v2.view toRect:v2.view.bounds];
_imageviewRight = [[UIImageView alloc] initWithImage:image2];
_imageviewRight.frame = CGRectMake(_imageviewRight.frame.origin.x + [UIScreen mainScreen].bounds.size.width, 0, _imageviewRight.frame.size.width, _imageviewRight.frame.size.height);
[self.view addSubview:_imageviewRight];
注意这里面的关键方法imageByCropping
//与pan结合使用 截图方法,图片用来做动画
-(UIImage*)imageByCropping:(UIView*)imageToCrop toRect:(CGRect)rect
{
CGFloat scale = [[UIScreen mainScreen] scale];
CGSize pageSize = CGSizeMake(scale*rect.size.width, scale*rect.size.height) ;
UIGraphicsBeginImageContext(pageSize);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
CGContextRef resizedContext =UIGraphicsGetCurrentContext();
CGContextTranslateCTM(resizedContext,-1*rect.origin.x,-1*rect.origin.y);
[imageToCrop.layer renderInContext:resizedContext];
UIImage*imageOriginBackground =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageOriginBackground = [UIImage imageWithCGImage:imageOriginBackground.CGImage scale:scale orientation:UIImageOrientationUp];
return imageOriginBackground;
}
- (void)viewDidDisappear:(BOOL)animated{
/********用于移除pan时的左右两边的view********/
[_imageviewLeft removeFromSuperview];
[_imageviewRight removeFromSuperview];
/********用于移除pan时的左右两边的view********/
}