//在TabBar上加手势
-(void)setOpenGestury
{
//判断tabbar上的手势为空就创建
if([self.view.gestureRecognizers count] ==0)
{
//加左右滑手势
UISwipeGestureRecognizer* recognizer =nil;
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(selectNextPage:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.viewaddGestureRecognizer:recognizer];
[recognizerrelease];
recognizer = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(selectNextPage:)];
[recognizersetDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.viewaddGestureRecognizer:recognizer];
[recognizerrelease];
}
}
//手势触发
-(void)selectNextPage:(UISwipeGestureRecognizer*)recognizer
{
UIViewController* curViewC =self.selectedViewController;
if([curViewCisKindOfClass:[UINavigationController class]])
{
intsubCount = [((UINavigationController*)curViewC).viewControllers count];
if(subCount >1)
{
//pop
if(recognizer.direction==UISwipeGestureRecognizerDirectionRight)
{
[((UINavigationController*)curViewC)popViewControllerAnimated:YES];
}
return;
}
}
NSArray* ary =self.viewControllers;
if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft)
{
for(intiIndex =1;iIndex < ary.count;iIndex++)
{
intnextIndex = (self.selectedIndex+ iIndex)%ary.count;
UIViewController* c = [aryobjectAtIndex:nextIndex];
BOOLcouldClick =YES;
if([self.delegaterespondsToSelector:@selector(tabBarController:shouldSelectViewController:)])
{
couldClick = [self.delegate tabBarController:selfshouldSelectViewController:c];
}
if(!couldClick)
{
continue;
}
else
{
[selfachieveAnimation:self.selectedIndextoIndex:nextIndexrecognizerDirection:UISwipeGestureRecognizerDirectionLeft];
self.selectedIndex= nextIndex;
break;
}
}
}
else if(recognizer.direction==UISwipeGestureRecognizerDirectionRight)
{
for(intsearchCount =1; searchCount < ary.count; searchCount++)
{
intnextIndex = (self.selectedIndex- searchCount + ary.count)%ary.count;
UIViewController* c = [ary objectAtIndex:nextIndex];
BOOLcouldClick =YES;
if([self.delegaterespondsToSelector:@selector(tabBarController:shouldSelectViewController:)])
{
couldClick = [self.delegate tabBarController:selfshouldSelectViewController:c];
}
if(!couldClick)
{
continue;
}
else
{
[selfachieveAnimation:self.selectedIndextoIndex:nextIndexrecognizerDirection:UISwipeGestureRecognizerDirectionRight];
self.selectedIndex= nextIndex;
break;
}
}
}
}
//截图方法,图片用来做动画
-(UIImage*)imageByCropping:(UIView*)imageToCrop toRect:(CGRect)rect
{
CGFloatscale = [[UIScreen mainScreen]scale];
CGSizepageSize =CGSizeMake(scale*rect.size.width, scale*rect.size.height) ;
UIGraphicsBeginImageContext(pageSize);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
CGContextRefresizedContext =UIGraphicsGetCurrentContext();
CGContextTranslateCTM(resizedContext,-1*rect.origin.x,-1*rect.origin.y);
[imageToCrop.layerrenderInContext:resizedContext];
UIImage*imageOriginBackground =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageOriginBackground = [UIImage imageWithCGImage:imageOriginBackground.CGImage scale:scaleorientation:UIImageOrientationUp];
returnimageOriginBackground;
}
//实现动画效果
-(void)achieveAnimation:(int)originIndex toIndex:(int)toIndex recognizerDirection:(UISwipeGestureRecognizerDirection)direction
{
if(originIndex <0|| originIndex >=self.viewControllers.count)
{
EZGAssert(0);
return;
}
if(toIndex <0|| toIndex >=self.viewControllers.count)
{
EZGAssert(0);
return;
}
UIViewController* v1 = [self.viewControllers objectAtIndex:originIndex];
UIViewController* v2 = [self.viewControllers objectAtIndex:toIndex];
UIImage* image1 = [selfimageByCropping:v1.viewtoRect:v1.view.bounds];
UIImage* image2 = [selfimageByCropping:v2.viewtoRect:v1.view.bounds];
if(image1 ==nil|| image2 ==nil)
{
EZGAssert(0);
return;
}
UIImageView* imageview1 = [[UIImageView alloc]initWithImage:image1];
UIImageView* imageview2 = [[UIImageView alloc]initWithImage:image2];
UIWindow* window = [UIApplication sharedApplication].keyWindow;
CGRectrectV1 = [v1.view convertRect:v1.view.bounds toView:window];
//CGRect rectV2 = [v2.view convertRect:v1.view.bounds toView:window];
//set possion
imageview1.frame= rectV1;
imageview2.frame= rectV1;
[windowaddSubview:imageview1];
[windowaddSubview:imageview2];
window.userInteractionEnabled=NO;
floatscreenWidth = [UIScreen mainScreen].bounds.size.width;
if(direction ==UISwipeGestureRecognizerDirectionLeft)
{
//to left
imageview2.center=CGPointMake(imageview1.center.x+ screenWidth, imageview1.center.y);
}
else
{
//to right
imageview2.center=CGPointMake(imageview1.center.x- screenWidth, imageview1.center.y);
}
[UIView animateWithDuration:0.3animations:^{
if(direction ==UISwipeGestureRecognizerDirectionLeft)
{
//to left
imageview1.center=CGPointMake(imageview1.center.x- screenWidth, imageview1.center.y);
imageview2.center=CGPointMake(imageview2.center.x- screenWidth, imageview1.center.y);
}
else
{
//to right
imageview1.center=CGPointMake(imageview1.center.x+ screenWidth, imageview1.center.y);
imageview2.center=CGPointMake(imageview2.center.x+ screenWidth, imageview1.center.y);
}
}completion:^(BOOLfinished)
{
[imageview1removeFromSuperview];
[imageview2removeFromSuperview];
window.userInteractionEnabled=YES;
}];
}