Swift常见TabBar多次点击处理Demo

Swift 里的 TabBar 一直挺绕的,尤其你想搞点不那么“系统”的行为,比如多次点击同一个 Tab 触发刷新、弹窗或者别的交互。

TabBarController 的多 Tap 响应这个小 Demo,刚好帮你把思路理清楚了,还带代码,直接上手调试也方便。

UITabBarController本身就挺好用的,默认切换、管理多个页面都流畅。但你要自定义,比如点第二次 Tab 不跳转页面而是刷新内容,就得动点手脚了。

方法比较简单,创建一个自己的CustomTabBarController子类,重写tabBar(_:didSelect:)或者监听选中状态的变化。加点判断和Timer逻辑,多 Tap 就能搞定。

项目结构也比较清晰:CustomTabBarController.swift负责主逻辑,每个 Tab 配一个ViewController.swift,资源和Main.storyboard也都准备好了,拖进去直接能跑。

要注意的是多 Tap 的判断,不建议用时间戳对比太复杂,Swift 里用DispatchSourceTimer或 GCD 做个节流就挺靠谱的。

如果你 Tab 那块功能比较多,比如点一下刷新、连点两下回到首页、点三下弹个提示,或者你想实现类似微信那种“连点消息 Tab 自动滚动到顶部”的交互,这套逻辑就合适。

另外你也可以拓展下,比如加个UISwipeGestureRecognizer实现 Tab 间滑动切换,或者引入Combine做响应式绑定,效果更丝滑。

想进一步了解自定义 TabBar 效果或控件的,可以参考:自定义 UITabbarController 的 tabBar 界面效果自定义 TabBar 控件,里面细节讲得也挺全。

,如果你正在用 Swift 写 App、想做点不那么“标准”的交互,这个小 Demo 还是蛮值得一看的。

zip
swift-常见的TabBar框架多Tap处理的小Demo.zip 预估大小:97个文件
folder
TQ_FrameDemo-master 文件夹
folder
TQ_FrameDemo 文件夹
file
TQFrameDemoTabBar.h 227B
file
TQNavigationController.m 4KB
folder
Message 文件夹
folder
Controllers 文件夹
file
TQMessageViewController.h 235B
file
TQMessageViewController.m 858B
file
main.m 340B
file
ViewController.m 556B
file
AppDelegate.h 283B
folder
HomePage 文件夹
folder
Controllers 文件夹
file
TQHomeViewController.h 231B
file
TQRightTableViewController.h 248B
file
TQRightTableViewController.m 3KB
file
TQLeftTableViewController.h 246B
file
TQLeftTableViewController.m 3KB
file
TQContentScrollView.m 1004B
file
TQHomeViewController.m 6KB
file
TQContentScrollView.h 301B
folder
My 文件夹
folder
Controllers 文件夹
file
TQPersonalMainViewController.m 3KB
file
TQSettingViewController.h 235B
file
TQMyCenterViewController.h 237B
file
TQPersonalMainViewController.h 245B
file
TQSettingViewController.m 946B
file
TQMyCenterViewController.m 2KB
folder
Views 文件夹
file
TQPersonalTitleView.m 4KB
file
TQPersonalTitleView.h 389B
file
Info.plist 2KB
file
AppDelegate.m 2KB
file
TQNavigationController.h 241B
folder
Found 文件夹
folder
Controllers 文件夹
file
AVPViewController.m 5KB
file
AVPViewController.h 225B
file
ViewController.h 221B
folder
Base.lproj 文件夹
file
LaunchScreen.storyboard 2KB
file
Main.storyboard 2KB
file
TQFrameDemoTabBar.m 4KB
folder
Assets.xcassets 文件夹
folder
setting.imageset 文件夹
file
mine-setting-icon.png 1KB
file
mine-setting-icon@2x.png 2KB
file
Contents.json 477B
file
mine-setting-icon@3x.png 2KB
folder
TabBar 文件夹
folder
tabbar_home_highlighted.imageset 文件夹
file
tabbar_home_highlighted.png 336B
file
tabbar_home_highlighted@2x.png 569B
file
Contents.json 495B
file
tabbar_home_highlighted@3x.png 886B
folder
tabbar_profile.imageset 文件夹
file
tabbar_profile@2x.png 654B
file
Contents.json 468B
file
tabbar_profile@3x.png 1KB
file
tabbar_profile.png 345B
folder
tabbar_profile_highlighted.imageset 文件夹
file
tabbar_profile_highlighted@3x.png 1KB
file
Contents.json 504B
file
tabbar_profile_highlighted.png 370B
file
tabbar_profile_highlighted@2x.png 705B
folder
tabbar_discover_highlighted.imageset 文件夹
file
tabbar_discover_highlighted.png 322B
file
Contents.json 507B
file
tabbar_discover_highlighted@3x.png 1KB
file
tabbar_discover_highlighted@2x.png 686B
folder
tabBar_publish_icon.imageset 文件夹
file
tabBar_publish_icon@3x.png 7KB
file
Contents.json 414B
file
tabBar_publish_icon@2x.png 5KB
file
tabBar_publish_icon.png 4KB
folder
tabBar_publish_icon_highlighted.imageset 文件夹
file
tabBar_publish_click_icon@2x.png 5KB
file
tabBar_publish_click_icon@3x.png 7KB
file
Contents.json 432B
file
tabBar_publish_click_icon.png 4KB
folder
tabbar_background.imageset 文件夹
file
Contents.json 386B
file
tabbar_background@2x.png 1011B
file
Contents.json 62B
folder
tabbar_home.imageset 文件夹
file
tabbar_home.png 305B
file
Contents.json 459B
file
tabbar_home@3x.png 788B
file
tabbar_home@2x.png 515B
folder
tabbar_message_center.imageset 文件夹
file
tabbar_message_center@3x.png 659B
file
tabbar_message_center@2x.png 427B
file
Contents.json 489B
file
tabbar_message_center.png 224B
folder
tabbar_discover.imageset 文件夹
file
tabbar_discover@3x.png 1KB
file
tabbar_discover@2x.png 634B
file
Contents.json 471B
file
tabbar_discover.png 302B
folder
tabbar_message_center_highlighted.imageset 文件夹
file
tabbar_message_center_highlighted@3x.png 736B
file
tabbar_message_center_highlighted@2x.png 478B
file
Contents.json 525B
file
tabbar_message_center_highlighted.png 242B
folder
AppIcon.appiconset 文件夹
file
Contents.json 1KB
folder
back.imageset 文件夹
file
back@2x.png 1KB
file
back@3x.png 2KB
file
Contents.json 369B
file
back.png 2KB
file
Contents.json 62B
file
.DS_Store 6KB
file
README.md 919B
folder
TQ_FrameDemoUITests 文件夹
file
TQ_FrameDemoUITests.m 1KB
file
Info.plist 680B
folder
TQ_FrameDemoTests 文件夹
file
Info.plist 680B
file
TQ_FrameDemoTests.m 920B
folder
TQ_FrameDemo.xcodeproj 文件夹
folder
project.xcworkspace 文件夹
file
contents.xcworkspacedata 157B
folder
xcuserdata 文件夹
folder
tq1.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 34KB
file
project.pbxproj 32KB
folder
xcuserdata 文件夹
folder
tq1.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
xcschememanagement.plist 664B
file
TQ_FrameDemo.xcscheme 4KB
folder
xcdebugger 文件夹
file
Breakpoints_v2.xcbkptlist 91B
...
zip 文件大小:137.83KB