iOS 自定义滑动切换TabBar

貌似经常会用到,自己整理收藏起来,方便日后查找备用。

效果如图:

由于制作gif,调整了属性,所以看起来的效果不好。如果用默认配置,生成的gif会很大。

制作gif:

1.使用QuickTimePlayer ,mac上插上iPhone, 然后进行屏幕录制,但是要选择插上的iPhone,然后会自动在Mac弹出同步的iPhone屏幕,点击录制。

2.使用GIFBrewery,选中录制文件*.mov, 进行生成gif。

代码如下:

#import "XPBaseView.h"

@protocol XPDetailTabBarViewDelegate;

@interface XPDetailTabBarView : XPBaseView

@property (nonatomic,weak) id<XPDetailTabBarViewDelegate> delegate;
/**
 *  是否是双语
 */
@property (nonatomic,assign) BOOL isBilingual;
/**
 *  是否点赞
 */
@property (nonatomic,assign) BOOL isLike;

@end

@protocol XPDetailTabBarViewDelegate <NSObject>

@optional
- (void)XPDetailTabBarView:(XPDetailTabBarView*)view clickIndex:(NSInteger)index;

@end

  

#import "XPDetailTabBarView.h"

@interface XPDetailTabBarView ()

@property (nonatomic,strong) NSArray *dataImages;
@property (nonatomic,strong) UIView *moveView;
@property (nonatomic,assign) NSInteger moveIndex;

@end

@implementation XPDetailTabBarView

-(void)initData{

}

-(void)initSubViews{
    self.dataImages = @[@"detail_tab_english",@"icon_like",@"detail_tab_recording",@"detail_tab_quiz",@"detail_tab_myrecord"];

    CGFloat itemWidth = SCREEN_WIDTH / self.dataImages.count;
    CGFloat itemHeight = 44;

    self.moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, itemWidth, itemWidth)];
    self.moveView.backgroundColor = AppStyleThemeOrangeColor;
    [self addSubview:self.moveView];

    for (int i = 0; i<self.dataImages.count; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 20000+i;
        button.frame = CGRectMake(i*itemWidth, 0, itemWidth, itemHeight);
        button.backgroundColor = [UIColor clearColor];
        [button setImage:[UIImage imageNamed:[self.dataImages objectAtIndex:i]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
}

-(void)setMoveIndex:(NSInteger)moveIndex{

    NSInteger animationType = 1;//1左,2右
    if(moveIndex > _moveIndex){
        animationType = 2;
    }

    _moveIndex = moveIndex;

    CGFloat itemWidth = SCREEN_WIDTH / self.dataImages.count;
    CGFloat itemHeight = 44;

    CGRect rect = CGRectMake(moveIndex*itemWidth, 0, itemWidth, itemHeight);

    WS(weakSelf)
    [UIView animateWithDuration:0.2 animations:^{
        if (animationType == 2){//右
            weakSelf.moveView.frame = CGRectMake(rect.origin.x+2, rect.origin.y, rect.size.width, rect.size.height);
        } else if (animationType == 1){//左
            weakSelf.moveView.frame = CGRectMake(rect.origin.x-2, rect.origin.y, rect.size.width, rect.size.height);
        }
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            if (animationType == 2){//右
                weakSelf.moveView.frame = CGRectMake(rect.origin.x-5, rect.origin.y, rect.size.width, rect.size.height);
            } else if (animationType == 1){//左
                weakSelf.moveView.frame = CGRectMake(rect.origin.x+5, rect.origin.y, rect.size.width, rect.size.height);
            }
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.4 animations:^{
                weakSelf.moveView.frame = rect;
            } completion:^(BOOL finished) {

            }];
        }];
    }];
}

- (void)setIsBilingual:(BOOL)isBilingual{
    _isBilingual = isBilingual;

    UIButton *button = [self viewWithTag:20000];
    if(isBilingual){
        [button setImage:[UIImage imageNamed:@"detail_tab_english"] forState:UIControlStateNormal];
    }else{
        [button setImage:[UIImage imageNamed:@"detail_tab_bilingual"] forState:UIControlStateNormal];
    }
}

- (void)setIsLike:(BOOL)isLike{
    _isLike = isLike;

    UIButton *button = [self viewWithTag:20001];
    if(isLike){
        [button setImage:[UIImage imageNamed:@"icon_like_selected"] forState:UIControlStateNormal];
    }else{
        [button setImage:[UIImage imageNamed:@"icon_like"] forState:UIControlStateNormal];
    }
}

- (void)buttonClicked:(UIButton*)button{
    NSInteger index = button.tag - 20000;

    self.moveIndex = index;

    if(self.delegate && [self.delegate respondsToSelector:@selector(XPDetailTabBarView:clickIndex:)]){
        [self.delegate XPDetailTabBarView:self clickIndex:index];
    }
}
@end

  

时间: 2024-08-24 05:03:06

iOS 自定义滑动切换TabBar的相关文章

iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。

UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记录一下 目标效果: 并有切换效果,但是并没说清楚,具体切换效果,比如粘滞,弹性? 于是我做了一个弹性的. 看实现效果 一. 原理: (1)普通切换选择效果,直接贴在了tabbar上,tabbar再自定义处理图层 (2)触发事件是tabbar上的,没有图片而已.这么处理也是取巧了,降低了整体自定义难度

IOS 自定义 滑动返回 手势

/** 只需要在你自定义的导航控制器中,改成如下代码即可,自定义手势返回 */ #define KEY_WINDOW [[UIApplication sharedApplication] keyWindow] #define kScreenW KEY_WINDOW.bounds.size.width #define kAnimateDuration 0.3 #define kDefaultAlapa 0.5 #define kDefaultScale 0.95 #define iOS7 ([UI

IOS自定义场景切换动画。

IOS中我们可以通过Storyborad以及segue来实现我们自己的场景切换动画,新建项目使用Single View Application模板并取名为MyCustomSegue. 使用storyboard托出另一UIViewController并分设置两个控制器的视图颜色,并设置跳转页面的segue为custom 设置如图 新建文件MyCustomChangeSegue并重新perform方法 1 @implementation MyCustomChangeSegue 2 3 -(void)

关于tabbarViewController的左右滑动切换

在我们开发的过程中,有时会有左右侧滑tabbarViewController切换控制器的需求,在我自己做项目的时候遇到了此类需求,现在就在此记录一下我当时的做法,废话不多说,直接上代码: 在iOS7.0以前,要实现这样的效果,只有自定义TabBar了,但这很麻烦.而在iOS7.0以后,苹果在UITabBarControllerDelegate中增加了下面两个代理方法: - (nullable id <UIViewControllerInteractiveTransitioning>)tabBa

iOS 自定义页面的切换动画与交互动画

在iOS7之前,开发者为了寻求自定义Navigation Controller的Push/Pop动画,只能受限于子类化一个UINavigationController,或是用自定义的动画去覆盖它.但是随着iOS7的到来,Apple针对开发者推出了新的工具,以更灵活地方式管理UIViewController切换. 自定义导航栏的Push/Pop动画 为了在基于UINavigationController下做自定义的动画切换,先建立一个简单的工程,这个工程的rootViewController是一个

iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 的解决办法

iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar  的解决办法 问题:iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 1.自定义代码: - (void)viewWillAppear:(BOOL)animated { [super  viewWillAppear:animated]; // 删除系统自动生成的UITabBarB

iOS开发之多表视图滑动切换示例(仿&quot;头条&quot;客户端)

好长时间没为大家带来iOS开发干货的东西了,今天给大家分享一个头条新闻客户端各个类别进行切换的一个示例.在Demo中对所需的组件进行的简单封装,在封装的组件中使用的是纯代码的形式,如果想要在项目中进行使用,稍微进行修改即可. 废话少说,先介绍一下功能点,下图是整个Demo的功能点,最上面左边的TabBarButtonItem是用来减少条目的,比如下图有三个按钮,点击减号会减少一个条目.右边的为增加一个条目.点击相应的按钮是切换到对应的表视图上,下方红色的是滑动的指示器,同时支持手势滑动.运行具体

[iOS]通过UIScrollView和UIPageControl实现滑动切换的效果

UIPageControl是自带的控件,可以查看官方文档,下载官方示例学习. 如果对Xcode自带的文档不熟悉可以参见:苹果Xcode帮助文档阅读指南 接下来是我学习笔记,使用Storyboard实现滑动切换的效果. ----------------------------------------------------------------------------- 新建一个项目,拖上一个UIScrollView和UIPageControl,并且建立关联: 新建一个ContentViewCo

Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现

嘿嘿嘿,关于android滑动的操作,是不是经常都会用到呢. 我肯定也要学习一下啦. https://blog.csdn.net/u013184970/article/details/82882107 https://blog.csdn.net/qq_35820350/article/details/82460376 在网上学习了一下,这两篇文章写的不错. 来看一下效果 共有4各部分 1.自定义顶部栏 2.侧滑菜单 3.弹出菜单 4.标签滑动切换 进入具体实现环节啦 第一 .自定义顶部栏 1.先