initWithCoder vs initWithNib vs initWithStyle

本文介绍了三种初始化视图控制器的方法:initWithCoder用于从Storyboard中加载视图控制器;initWithNib用于从nib文件中加载视图控制器;initWithStyle则用于手动创建视图控制器。

initWithCoder⽤用于从storyboard中加载视图控制器,

initWithNib⽤用于从nib⽂文件中加载视图控制器,

initWithStyle则⽤用于⼿手动创建视图控制器

好的,我们一起来看这段代码 —— `TPBaffleView.h`,它是 `SDNAPPSwitchBaffleView` 的父类,是一个 **遮罩视图(Baffle View)**,用于实现弹窗、浮层等场景的背景模糊或半透明遮罩。 --- ## 📌 文件结构概览 ```objc @interface TPBaffleView : UIView - (instancetype)init; - (instancetype)initWithEffect:(UIBlurEffectStyle)blurEffectStyle; - (instancetype)initWithHostView:(UIView *)hostView effect:(UIBlurEffectStyle)blurEffectStyle; - (instancetype)initWithHostView:(UIView *)hostView; - (void)setBackgroundColor:(UIColor *)backgroundColor; - (void)show; - (void)showImmediately; - (void)showImmediatelyWithOffset:(CGFloat)offset; - (void)dismiss; @property(nonatomic, readonly, strong)UIView * contentView; - (instancetype)initWithCoder:(NSCoder *)aDecoder NS_UNAVAILABLE; - (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; @end ``` --- ## 🔍 类结构分析 ### ✅ 类名与继承关系 - **类名:** `TPBaffleView` - **继承自:** `UIView` - **用途:** 提供一个可带模糊效果的遮罩层,常用于弹窗、浮层等场景。 --- ## 🧱 初始化方法分析 ### 1. 默认初始化 ```objc - (instancetype)init; ``` - 使用默认模糊样式(可能是 `UIBlurEffectStyleLight` 或固定值)初始化遮罩视图。 --- ### 2. 指定模糊样式初始化 ```objc - (instancetype)initWithEffect:(UIBlurEffectStyle)blurEffectStyle; ``` - 允许开发者传入模糊效果样式(如 `UIBlurEffectStyleDark`, `UIBlurEffectStyleExtraLight` 等)。 --- ### 3. 绑定宿主视图并指定模糊样式 ```objc - (instancetype)initWithHostView:(UIView *)hostView effect:(UIBlurEffectStyle)blurEffectStyle; ``` - 与某个“宿主视图”绑定,遮罩层会添加在宿主视图之上。 - 常用于在某个视图上弹出遮罩层。 --- ### 4. 仅绑定宿主视图 ```objc - (instancetype)initWithHostView:(UIView *)hostView; ``` - 使用默认模糊样式初始化,并绑定宿主视图。 --- ## 🎨 方法与属性 ### 1. 设置背景颜色 ```objc - (void)setBackgroundColor:(UIColor *)backgroundColor; ``` - 覆盖父类方法,可能用于在模糊层之上叠加半透明颜色。 --- ### 2. 显示遮罩层(带动画) ```objc - (void)show; ``` - 显示遮罩层,通常带有淡入动画。 --- ### 3. 立即显示遮罩层(无动画) ```objc - (void)showImmediately; ``` - 无动画直接显示。 --- ### 4. 立即显示并带偏移量 ```objc - (void)showImmediatelyWithOffset:(CGFloat)offset; ``` - 常用于遮罩层从某个方向滑入(如从底部滑入)。 --- ### 5. 隐藏遮罩层 ```objc - (void)dismiss; ``` - 隐藏遮罩层,可能带有淡出动画。 --- ### 6. 内容容器视图 ```objc @property(nonatomic, readonly, strong)UIView * contentView; ``` - 提供一个 `contentView`,用于添加子视图(如弹窗内容)。 - 所有子视图应添加到这个 `contentView` 上,而不是直接加在 `TPBaffleView` 上。 --- ## ❌ 禁用的初始化方法 ```objc - (instancetype)initWithCoder:(NSCoder *)aDecoder NS_UNAVAILABLE; - (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; ``` - 禁止使用 `initWithFrame:` 和 `initWithCoder:` 初始化。 - 目的:强制使用自定义的初始化方法,确保遮罩层的正确创建逻辑。 --- ## 📌 总结:这个类的用途和设计意图 | 特性 | 说明 | |------|------| | 类型 | 自定义 UIView 子类 | | 功能 | 提供遮罩层 + 模糊背景 | | 支持动画 | 显示/隐藏动画 | | 容器支持 | 提供 contentView 添加子视图 | | 初始化方式 | 多种构造方式,支持绑定宿主视图 | | 使用场景 | 弹窗遮罩、浮层背景、模糊背景弹出框等 | --- ## ✅ 示例使用方式(伪代码) ```objc UIView *hostView = self.view; TPBaffleView *baffleView = [[TPBaffleView alloc] initWithHostView:hostView effect:UIBlurEffectStyleDark]; [baffleView.contentView addSubview:yourPopupView]; [baffleView show]; ``` --- ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值