`

扩展UITabBarController实现循环滚动滑动效果

阅读更多
扩展UITabBarController实现循环滑动效果

//在TabBar上加手势
-(void)setOpenGestury
{
    //判断tabbar上的手势为空就创建
    if ([self.view.gestureRecognizers count] == 0)
    {
        //加左右滑手势
        UISwipeGestureRecognizer* recognizer = nil;
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(selectNextPage:)];
        [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
        [self.view addGestureRecognizer:recognizer];
        [recognizer release];
       
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(selectNextPage:)];
        [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
        [self.view addGestureRecognizer:recognizer];
        [recognizer release];
    }
   
}
//手势触发
-(void)selectNextPage:(UISwipeGestureRecognizer*)recognizer
{
    UIViewController* curViewC = self.selectedViewController;
    if ([curViewC isKindOfClass:[UINavigationController class]])
    {
        int subCount = [((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 (int iIndex = 1 ;iIndex < ary.count ;iIndex++)
        {
            int nextIndex = (self.selectedIndex + iIndex)%ary.count;
           
            UIViewController* c = [ary objectAtIndex:nextIndex];
            BOOL couldClick = YES;
            if([self.delegaterespondsToSelector:@selector(tabBarController:shouldSelectViewController:)])
            {
                couldClick = [self.delegate tabBarController:self shouldSelectViewController:c];
            }
           
            if (!couldClick)
            {
                continue;
            }
            else
            {
               
                [self achieveAnimation:self.selectedIndex toIndex:nextIndexrecognizerDirection:UISwipeGestureRecognizerDirectionLeft];
                self.selectedIndex = nextIndex;
                break;
            }
           
        }
       
       
    }
    else if(recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
       
        for (int searchCount = 1; searchCount < ary.count; searchCount++)
        {
           int nextIndex = (self.selectedIndex - searchCount + ary.count)%ary.count;           
            UIViewController* c = [ary objectAtIndex:nextIndex];
            BOOL couldClick = YES;
            if([self.delegaterespondsToSelector:@selector(tabBarController:shouldSelectViewController:)])
            {
                couldClick = [self.delegate tabBarController:self shouldSelectViewController:c];
            }
            if (!couldClick)
            {
                continue;
            }
            else
            {
                [self achieveAnimation:self.selectedIndex toIndex:nextIndexrecognizerDirection:UISwipeGestureRecognizerDirectionRight];
                self.selectedIndex = nextIndex;
               
                break;
            }
        }
              
    }
   
}

//截图方法,图片用来做动画
-(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:scaleorientation:UIImageOrientationUp];
   
    return imageOriginBackground;
}
//实现动画效果
-(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 = [self imageByCropping:v1.view toRect:v1.view.bounds];
    UIImage* image2 = [self imageByCropping:v2.view toRect: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;
   
    CGRect rectV1 = [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;
   
    [window addSubview:imageview1];
    [window addSubview:imageview2];
   
    window.userInteractionEnabled = NO;
   
    float screenWidth = [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.3 animations:^{
        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:^(BOOL finished)
     {
         [imageview1 removeFromSuperview];
         [imageview2 removeFromSuperview];
         window.userInteractionEnabled = YES;
     }];
   
   
}
0
2
分享到:
评论

相关推荐

    swift-在系统UITabBarController的基础上实现安卓版微信TabBar的滑动切换功能

    为了实现类似微信TabBar的滑动切换效果,我们需要对其进行定制和扩展。 首先,我们需要了解`UITabBarController`的基本结构。它是一个管理多个`UIViewController`的容器控制器,每个`UIViewController`对应TabBar上...

    swift-SwipeableTabBarController-UITabBarController标签滑动互动

    Swift中的SwipeableTabBarController是一种实现UITabBarController标签页滑动互动的高级技术,它扩展了标准的苹果UIKit框架,提供了更丰富的用户交互体验。在iOS应用设计中,UITabBarController通常用于管理多个视图...

    swift-模拟UITabBarController的父子控制器效果实现父子控制器管理

    本教程将深入探讨如何使用Swift来模拟 `UITabBarController` 的父子控制器效果,实现更灵活的控制器管理。 首先,我们需要理解 `UITabBarController` 的核心特性:它包含一组 `tabBarItems`,每个 `tabBarItem` ...

    ios-自定义UITabBarController.zip

    4. **事件处理**:自定义的`TabBarController`可能包含了对触摸事件的处理,比如滑动切换标签页,或者通过手势识别器实现更多交互效果。 5. **子控制器管理**:`UITabBarController`会自动管理其子控制器,但自定义...

    UITabBarController和UINavigationController混用

    此外,我们还可以使用扩展 `UITabBarController` 的方式,创建一个公共方法来处理 `tabBar` 的隐藏和显示,使得代码更加整洁: ```swift extension UITabBarController { func hideTabBar(_ hidden: Bool) { ...

    自定义UITabBarController

    "自定义UITabBarController"旨在实现对这个组件的全面定制,包括但不限于标签的外观、选中状态的动画、图标颜色、文字样式等。这个项目,如"**BCTabBarController-master**"所示,可能提供了一个自定义解决方案,使...

    ios-UITabBarController.zip

    自定义UITabBarController

    iOS 自定义UINavigationController和UITabBarController

    这些示例可能包括创建自定义导航栏的Swift或Objective-C类,以及对`UINavigationController`和`UITabBarController`的扩展以覆盖其默认行为。开发者可以参考这些代码,根据自己的需求进行调整和集成。 总之,自定义...

    UItabbarController 简单使用Demo

    4. **自定义TabBar**:如果你需要自定义`UITabBar`的颜色、字体或选中效果,可以重写`UITabBarController`的相关方法或者使用`appearance`代理进行全局设置。 5. **运行项目**:现在,当你运行应用时,你会看到底部...

    ios-自定义UITabBarController,完美的搭建框架,可以直接用在项目中,还有完美的UIwebView.zip

    在iOS应用开发中,UITabBarController是苹果提供的一个标准组件,用于实现底部标签栏的切换效果,方便用户在多个视图控制器间进行导航。然而,系统默认的UITabBarController有时不能满足开发者对于个性化和功能扩展...

    UITabBarController Demo代码

    通过理解和实践`UITabBarControllerDemo`,开发者不仅可以掌握如何创建和管理`UITabBarController`,还能了解到如何自定义和扩展其功能,以满足不同应用的需求。这是一项基础但重要的技能,对于iOS应用开发来说必不...

    自定义UITabBarController的badge的外观

    通过以上步骤,我们就成功地自定义了`UITabBarController` 的`badge`外观,实现了类似微信小红点的效果。这种自定义方法不仅限于红点,还可以扩展到其他形状、颜色和动画效果,满足更多样化的界面设计需求。在实际...

    页面跳转 UITabBarController+UINavigationController+UIViewController

    `UITabBarController`用于实现底部标签栏切换不同功能模块,`UINavigationController`则管理着一个堆栈式的视图控制器序列,支持前进和后退操作,而`UIViewController`是所有自定义视图控制器的基础类,承载着具体的...

    IOS自定义UITabBarController(动画背影移动,效果非常好)

    本篇文章将深入探讨如何实现标题中描述的"IOS自定义UITabBarController(动画背影移动,效果非常好)",以及如何通过代码控制背景移动和动态效果。 首先,我们需要创建一个自定义的`UITabBarController`子类,例如命名...

    UINavigationController+UITabBarController框架

    我们写iOS项目的时候,基本都是一个UINavigationController套一个UITabBarController的形式,就是上面一个导航栏,下面几个按钮的工具条的形式。我写了几个应用,发现如果每次都重新写的话完全就是浪费精力和时间,...

    ios自定义UITabBarController

    本教程将深入探讨如何在iOS中自定义`UITabBarController`,以实现底部排列五个按钮,并且点击中间按钮能够跳转到新界面的功能。 首先,我们了解`UITabBarController`的基本结构。它是一个容器控制器,可以容纳多个`...

    uitabbarcontroller navigationcontroller互相加载导航demo

    在iOS应用开发中,`UITabBarController` 和 `UINavigationController` 是两个非常重要的组件,它们分别用于实现底部标签栏切换和页面的栈式导航。`UITabBarController` 通常用作应用的基础视图控制器,展示多个子...

    UITabBarController

    `UITabBarController`是iOS应用开发中的一个核心组件,它是苹果的UIKit框架的一部分,用于实现底部标签栏界面。这个组件允许用户在多个视图控制器之间切换,每个标签对应一个不同的功能或内容区域。在iOS应用设计中...

Global site tag (gtag.js) - Google Analytics