CustomTabBar 自定义选项卡视图

news2025/2/23 10:49:08

1. 用到的技术点

  1) Generics      泛型

  2) ViewBuilder   视图构造器

  3) PreferenceKey 偏好设置

  4) MatchedGeometryEffect 几何效果

2. 创建枚举选项卡项散列,TabBarItem.swift

import Foundation
import SwiftUI

//struct TabBarItem: Hashable{
//    let iconName: String
//    let title: String
//    let color: Color
//}

///枚举选项卡项散列
enum TabBarItem: Hashable{
    case home, favorites, profile, messages
    
    var iconName: String{
        switch self {
        case .home: return "house"
        case .favorites: return "heart"
        case .profile:   return "person"
        case .messages:  return "message"
        }
    }
    
    var title: String{
        switch self {
        case .home: return "Home"
        case .favorites: return "Favorites"
        case .profile:   return "Profile"
        case .messages:  return "Messages"
        }
    }
    
    var color: Color{
        switch self {
        case .home: return Color.red
        case .favorites: return Color.blue
        case .profile:   return Color.green
        case .messages:  return Color.orange
        }
    }
}

3. 创建选项卡偏好设置 TabBarItemsPreferenceKey.swift

import Foundation
import SwiftUI

/// 选项卡项偏好设置
struct TabBarItemsPreferenceKey: PreferenceKey{
    static var defaultValue: [TabBarItem] = []
    
    static func reduce(value: inout [TabBarItem], nextValue: () -> [TabBarItem]) {
        value += nextValue()
    }
}

/// 选项卡项视图修饰符
struct TabBarItemViewModifer: ViewModifier{
    let tab: TabBarItem
    @Binding var selection: TabBarItem
    
    func body(content: Content) -> some View {
        content
            .opacity(selection == tab ? 1.0 : 0.0)
            .preference(key: TabBarItemsPreferenceKey.self, value: [tab])
    }
}

extension View{
    /// 选项卡项视图修饰符
    func tabBarItem(tab: TabBarItem, selection: Binding<TabBarItem>) -> some View{
        modifier(TabBarItemViewModifer(tab: tab, selection: selection))
    }
}

4. 创建自定义选项卡视图 CustomTabBarView.swift

import SwiftUI

/// 自定义选项卡视图
struct CustomTabBarView: View {
    let tabs: [TabBarItem]
    @Binding var selection: TabBarItem
    @Namespace private var namespace
    @State var localSelection: TabBarItem
    
    var body: some View {
        //tabBarVersion1
        tabBarVersion2
            .onChange(of: selection) { value in
                withAnimation(.easeInOut) {
                    localSelection = value
                }
            }
    }
}

extension CustomTabBarView{
    /// 自定义 tabitem 布局
    private func tabView1(tab: TabBarItem) -> some View{
        VStack {
            Image(systemName: tab.iconName)
                .font(.subheadline)
            Text(tab.title)
                .font(.system(size: 12, weight: .semibold, design: .rounded))
        }
        .foregroundColor(localSelection == tab ? tab.color : Color.gray)
        .padding(.vertical, 8)
        .frame(maxWidth: .infinity)
        .background(localSelection == tab ? tab.color.opacity(0.2) : Color.clear)
        .cornerRadius(10)
    }
    
    /// 选项卡版本1
    private var tabBarVersion1: some View{
        HStack {
            ForEach(tabs, id: \.self) { tab in
                tabView1(tab: tab)
                    .onTapGesture {
                        switchToTab(tab: tab)
                    }
            }
        }
        .padding(6)
        .background(Color.white.ignoresSafeArea(edges: .bottom))
    }
    
    /// 切换选项卡
    private func switchToTab(tab: TabBarItem){
        selection = tab
    }
}

extension CustomTabBarView{
    /// 自定义 tabitem 布局 2
    private func tabView2(tab: TabBarItem) -> some View{
        VStack {
            Image(systemName: tab.iconName)
                .font(.subheadline)
            Text(tab.title)
                .font(.system(size: 12, weight: .semibold, design: .rounded))
        }
        .foregroundColor(localSelection == tab ? tab.color : Color.gray)
        .padding(.vertical, 8)
        .frame(maxWidth: .infinity)
        .background(
            ZStack {
                if localSelection == tab{
                    RoundedRectangle(cornerRadius: 10)
                        .fill(tab.color.opacity(0.2))
                        .matchedGeometryEffect(id: "background_rectangle", in: namespace)
                }
            }
        )
    }
    
    /// 选项卡版本 2
    private var tabBarVersion2: some View{
        HStack {
            ForEach(tabs, id: \.self) { tab in
                tabView2(tab: tab)
                    .onTapGesture {
                        switchToTab(tab: tab)
                    }
            }
        }
        .padding(6)
        .background(Color.white.ignoresSafeArea(edges: .bottom))
        .cornerRadius(10)
        .shadow(color: Color.black.opacity(0.3), radius: 10, x: 0, y: 5)
        .padding(.horizontal)
    }
}

struct CustomTabBarView_Previews: PreviewProvider {
    static let tabs: [TabBarItem] = [.home, .favorites, .profile]
    
    static var previews: some View {
        VStack {
            Spacer()
            CustomTabBarView(tabs: tabs, selection: .constant(tabs.first!), localSelection: tabs.first!)
        }
    }
}

5. 创建自定义选项卡容器视图 CustomTabBarContainerView.swift

import SwiftUI

/// 自定义选项卡容器视图
struct CustomTabBarContainerView<Content: View>: View {
    @Binding var selection: TabBarItem
    let content: Content
    @State private var tabs: [TabBarItem] = []
    
    init(selection: Binding<TabBarItem>, @ViewBuilder content: () -> Content){
        self._selection = selection
        self.content = content()
    }
    
    var body: some View {
        ZStack(alignment: .bottom) {
            content
                .ignoresSafeArea()
            CustomTabBarView(tabs: tabs, selection: $selection, localSelection: selection)
        }
        .onPreferenceChange(TabBarItemsPreferenceKey.self) { value in
            tabs = value
        }
    }
}

struct CustomTabBarContainerView_Previews: PreviewProvider {
    static let tabs: [TabBarItem] = [ .home, .favorites, .profile]
    
    static var previews: some View {
        CustomTabBarContainerView(selection: .constant(tabs.first!)) {
            Color.red
        }
    }
}

6. 创建应用选项卡视图 AppTabBarView.swift

import SwiftUI

// Generics      泛型
// ViewBuilder   视图构造器
// PreferenceKey 偏好设置
// MatchedGeometryEffect 几何效果

/// 应用选项卡视图
struct AppTabBarView: View {
    @State private var selection: String = "Home"
    @State private var tabSelection: TabBarItem = .home
                       
    var body: some View {
        /// 默认系统的 TabView
        // defaultTabView
        /// 自定义 TabView
        customTabView
    }
}

extension AppTabBarView{
    /// 默认系统的 TabView
    private var defaultTabView: some View{
        TabView(selection: $selection) {
            Color.red
                .ignoresSafeArea(edges: .top)
                .tabItem {
                    Image(systemName: "house")
                    Text("Home")
                }
            Color.blue
                .ignoresSafeArea(edges: .top)
                .tabItem {
                    Image(systemName: "heart")
                    Text("Favorites")
                }
            Color.orange
                .ignoresSafeArea(edges: .top)
                .tabItem {
                    Image(systemName: "person")
                    Text("Profile")
                }
        }
    }
    
    /// 自定义 TabView
    private var customTabView: some View{
        CustomTabBarContainerView(selection: $tabSelection) {
            Color.red
                .tabBarItem(tab: .home, selection: $tabSelection)
            
            Color.blue
                .tabBarItem(tab: .favorites, selection: $tabSelection)
            
            Color.green
                .tabBarItem(tab: .profile, selection: $tabSelection)
            
            Color.orange
                .tabBarItem(tab: .messages, selection: $tabSelection)
        }
    }
}

struct AppTabBarView_Previews: PreviewProvider {
    static var previews: some View {
        AppTabBarView()
    }
}

7. 效果图:

        

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1088283.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CV2对图像做渐变模糊

一段小代码&#xff0c;供参考&#xff0c;在CV2中做一些边缘渐变的功能。 主要流程&#xff1a; 生成一个随机的mask&#xff0c;并归一化&#xff0c;使其作为另一张图片的权重。对mask做高斯模糊&#xff0c;实现边缘渐变。将加权mask与原始图片相乘。 import os import cv…

HelloKitty 代码 Python

话不多说直接上代码,绘制速度慢,录屏之后调倍速 import math import turtle as t# 计算长度、角度 t1:画笔对象 r:半径 angle:扇形(圆形)的角度 def myarc(t1, r, angle):arc_length = 2 * math.pi * r * angle

树莓派安装.NET 6.0

首先安装.Net Core依赖&#xff08;未使用&#xff09; sudo apt install -y libunwind8 libunwind8-dev gettext libicu-dev liblttng-ust-dev libcurl4 libcurl4-openssl-dev libssl-dev uuid-dev unzip libgdiplus libc6-dev libkrb5-3 需要安装的依赖微软官方文档已经列出…

想要精准获客? 必看企业软文的写作技巧

信息碎片化时代下&#xff0c;企业获客的常用方式就是软文营销&#xff0c;因为软文相比于一般的营销方式成本更低&#xff0c;效果持续时间长&#xff0c;但是有许多企业在写软文时没有掌握好方法&#xff0c;认为文章只要发了就行&#xff0c;导致宣传效果不明显&#xff0c;…

字符串的左旋和判断一个字符串是否为另外一个字符串旋转之后的字符串。(C语言实现)

目录 1. 字符串的左旋 2. 判断一个字符串是否为另外一个字符串旋转之后的字符串 1. 字符串的左旋 题目&#xff1a; 实现一个函数&#xff0c;可以左旋字符串中的k个字符。 例如&#xff1a; ABCD左旋一个字符得到BCDA ABCD左旋两个字符得到CDAB 解析&#xff1a;该题有…

怎么把电影中的声音保存下来?

怎么把电影中的声音保存下来&#xff1f;当我们欣赏电影时&#xff0c;常常会遇到喜欢的片段、精彩的台词或优美的音乐。这时候&#xff0c;我们可能会有一个念头&#xff0c;希望能够将电影中的声音留存下来&#xff0c;以供将其作为手机铃声或背景音乐使用。 要实现这个目标&…

盘点:数字人直播系统源码部署哪家好?

数字人直播克隆系统是一种利用人工智能技术&#xff0c;将真实人物的形象、声音、表情、动作等特征转化为数字化的虚拟人物&#xff0c;通过网络进行实时互动的新型媒体形式。数字人直播克隆系统源码部署是指将数字人直播系统的核心代码和模型安装在自己的服务器上&#xff0c;…

【OAuth】OAuth2.0四种授权模式

什么是OAuth&#xff1f; 开放授权&#xff08;OAuth&#xff09;是一个开放标准&#xff0c;允许用户让第三方应用访问该用户在某一网站上存储私密的资源&#xff08;如照片&#xff0c;视频&#xff0c;联系人列表&#xff09;&#xff0c;而无需将用户名和密码提供给第三方…

和为 K 的子数组

题目链接 和为 K 的子数组 题目描述 注意点 -1000 < nums[i] < 1000子数组是数组中元素的连续非空序列 解答思路 最初想到的思路是使用递归&#xff0c;遍历整个数组&#xff0c;当访问到idx位置处的元素时&#xff0c;可以根据idx - 1作为末尾元素的子数组和推出id…

SAP router的问题 dev_out 大文件 ,bat 关闭服务,删除文件,重启服务

跟老师确认后&#xff0c;dev_out可以删除 具体时先把sap-router停掉&#xff0c;删除dev_out 重启服务 问题&#xff1a; 1、问题是saprouter 不能停止&#xff0c;停止的话 外网都要用VPN&#xff0c;那就避开高峰时间 可以后半夜搞这个事情 2、如何定时执行 &#xff…

C++的高手之旅

要学习C并成为C大佬&#xff0c;以下是一些建议&#xff1a; 掌握C基础知识&#xff1a;C是一种面向对象的编程语言&#xff0c;它包含了C语言的大部分语法和特性。因此&#xff0c;学习C之前&#xff0c;建议先掌握C语言的基础知识&#xff0c;包括数据类型、控制流、指针、内…

详解cv2.copyMakeBorder函数【OpenCV图像边界填充Python版本】

文章目录 简介函数原型代码示例参考资料 简介 做深度学习图像数据集时&#xff0c;有时候需要调整一张图片的长和宽。如果直接使用cv2.resize函数会造成图像扭曲失真&#xff0c;因此我们可以采取填充图像短边的方法解决这个问题。cv2.copyMakeBorder函数提供了相关操作。本篇…

[Machine Learning][Part 5]监督学习——逻辑回归

之前文章中提到监督学习的应用可分为两类&#xff1a;线性回归和逻辑回归。和线性回归不同&#xff0c;逻辑回归输出只有0和1。对于一个逻辑回归任务&#xff0c;可以先使用线性回归来预测y。然而我们希望逻辑回归预测模型输出的是0和1&#xff0c;为了达到这个目的&#xff0c…

javascript将html中的dom元素转图片

javascript将html中的dom元素转图片 百度网盘下载html2canvas.min.js&#xff1a; 全部文件-》js插件-》 <!DOCTYPE html> <html><head><meta charset"utf-8"><title>网页中的某个区域转图片</title></head><body styl…

精准突击!GitHub星标103k,2023年整理1658页JAVA秋招面试题

前言&#xff1a; 现在的互联网开发岗招聘&#xff0c;程序员面试背八股文已经成为了不可逆转的形式&#xff0c;其中一个Java岗几百人在投简历也已经成为了常态&#xff01;更何况一份面试题动辄七八百道&#xff0c;你吃透了&#xff0c;技术只要不是很差&#xff0c;面试怎…

易点易动采购管理模块:实现全生命周期管理,助力企业采购管理的高效运作

在现代企业中&#xff0c;采购管理是一个关键的业务环节。然而&#xff0c;许多企业面临着采购流程繁琐、信息不透明、成本控制困难等问题&#xff0c;导致采购效率低下和资源浪费。为了解决这些问题&#xff0c;易点易动采购管理模块应运而生。本文将详细介绍易点易动采购管理…

PIL Image格式转Tensor

Image格式是由PIL库读入的图片格式 from PIL import Image torch.Tensor是用于深度学习计算的张量格式 import torch 1 Image格式转Tensor 先转numpy 再转tensor torch.from_numpy() np.asarray() image torch.from_numpy(np.asarray(image)) 但是报错: max_pool2d” not im…

PostGIS导入shp文件报错:dbf file (.dbf) can not be opened.

一、报错 刚开始以为是SRID输入错误&#xff0c;反复尝试SRID的输入&#xff0c;还是报错&#xff01; 后来看到了这篇博客&#xff0c;解决了&#xff01;https://blog.csdn.net/Fama_Q/article/details/117381378 二、导致报错的原因 导入的shp文件路径太深&#xff0c;换…

Maven系列第6篇:生命周期和插件详解?

maven系列目标&#xff1a;从入门开始开始掌握一个高级开发所需要的maven技能。 这是maven系列第6篇。 整个maven系列的内容前后是有依赖的&#xff0c;如果之前没有接触过maven&#xff0c;建议从第一篇看起&#xff0c;本文尾部有maven完整系列的连接。 前面我们使用maven…

AI 生成的唯美头像也太好看了吧!附好说 AI 一秒出图技巧

在注重线上社交的当下&#xff0c;拥有不一样的头像是提高个人辨识度的好方法。比起网上找图和人 “撞头像”&#xff0c;如今免费的 AI 生图或许是更多人的 “最优解”。 这里我们参考好说社区大家的作品&#xff0c;提炼了一些 AI 作图要点分享给大家。我们的目标很简单&…