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
iOS 自定义滑动切换TabBar的更多相关文章
- iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。
UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记 ...
- IOS自定义场景切换动画。
IOS中我们可以通过Storyborad以及segue来实现我们自己的场景切换动画,新建项目使用Single View Application模板并取名为MyCustomSegue. 使用storyb ...
- iOS 自定义UITabBarController的tabBar
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...
- iOS开发之多表视图滑动切换示例(仿"头条"客户端)---优化篇
前几天发布了一篇iOS开发之多表视图滑动切换示例(仿"头条"客户端)的博客,之所以写这篇博客,是因为一位iOS初学者提了一个问题,简单的写了个demo做了个示范,让其在基础上做扩展 ...
- Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现
嘿嘿嘿,关于android滑动的操作,是不是经常都会用到呢. 我肯定也要学习一下啦. https://blog.csdn.net/u013184970/article/details/82882107 ...
- 【iOS自定义键盘及键盘切换】详解
[iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...
- iOS彩票项目--第五天,新特性引导页的封装、返回按钮的自定义、导航控制器的滑动返回以及自定义滑动返回功能
一.上次实现了在AppDelegate中通过判断app版本决定是否进入新特性页面,今天将AppDelegate中的一坨进行了封装.将self.window的根控制器到底应该为新特性界面,还是主页面,封 ...
- WPF 自定义滑动ScrollViewer
自定义滑动滚动条 预期目标:横向滚动条,可以左右滑动,用鼠标按住(触摸)然后释放可以实现快速滑动. 我们有几种方案: 1.ScrollViewer,修改其中的横向滚动条,将其中的背景设置为透明. 但是 ...
- FlexSlider jQuery滑动切换插件 参数
demo:http://www.sucaihuo.com/jquery/0/6/demo/ FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所 ...
随机推荐
- SQLLDR记录数与文本记录数比较
我们平时都用sqlldr进行将文本数据加载到数据库,但是有时候由于数据问题导致入库率不能达到100%,因此我们要检测是否存在不能入库的数据记录.以下shell脚本就是统计文本中记录数和数据库中记录数是 ...
- (转)【ASP.NET Web API】Authentication with OWIN
概述 本文说明了如何使用 OWIN 来实现 ASP.NET Web API 的验证功能,以及在客户端与服务器的交互过程中,避免重复提交用户名和密码的机制. 客户端可以分为两类: JavaScript: ...
- Web程序的桌面提醒
做web开发常会面对的一个问题是,浏览器最小化的时候如何才能向用户发送通知.解决办法大概有三种:让用户有事没事儿搂两眼页面:开发一个桌面客户端:Html5的Notification API. 目前没看 ...
- oracle ORA-00911 问题 解决
书写sql语句 using (OracleConnection conn = new OracleConnection(OracleString)) { conn.Open(); var trans ...
- ContentControl 与 ViewModel (一)
前阵子有人问我MVVM模式下,在View中嵌套View,切换View.想一想还是写下来吧. 主要就是用到 ContentControl 和 DataTemplate,这算是一种 ViewModel F ...
- windows平台下redis安装及配置文件介绍
1.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...
- sqlserver 链接 ODBC 访问 MySql
环境:windows 2008 + sqlserver 2008 一 安装 mysql-connector-odbc-5.2.5-winx64.msi 必须安装5.2.5,安装mysql-connec ...
- load css use javascript
var $ = document; // shortcut var cssId = 'myCss'; // you could encode the css path itself to genera ...
- JPA 使用
本文以JPA+Hibernate 角色与权限示例说明. 角色实体定义: @Entity @Table public class Role { private long id; private Stri ...
- windows装了双系统设置默认启动系统
我们装了双系统后,在开机设置时会自动启动其中一个系统,有时提示时间会很短,以至于不能判断自己是否装了双系统,以下,当电脑打开后,我们可以观察是否装了双系统 1.按组合键<Win+R>,打开 ...