IOS开源:HHBadgeHUD-一款更 Q 的通知图标控件(OC)
Kim8815
7年前
<h2>简介</h2> <p>一款更Q的通知图标控件。</p> <h2>演示项目</h2> <p>查看并运行 HHBadgeHUDDemo/HHBadgeHUDDemo.xcodeproj</p> <p><img src="https://simg.open-open.com/show/7bb193a7792773a896ca067d66758e66.gif"></p> <h2>特性</h2> <ul> <li><strong>无侵入性</strong> :</li> <li><strong>轻量</strong> :</li> <li><strong>易扩展</strong> :</li> </ul> <h2>使用方法</h2> <p>###数字图标样式</p> <pre> <code class="language-objectivec">// 1. 给UIView添加通知图标 self.oneView.hh_badge = [HHCountBadge badgeWithCount:99]; // 配置外观样式 [(HHCountBadge *)self.oneView.hh_badge applyStyle:^(HHCountBadgeStyle *style) { style.bgColor = [UIColor cyanColor]; style.borderColor = [UIColor redColor]; style.borderWidth = 1; ... }]; // 向左移5, 向下移5 [(HHCountBadge *)self.oneView.hh_badge moveByX:-5 Y:5]; // 角标+1 [(HHCountBadge *)self.oneView.hh_badge increaseWithAnimationType:HHBadgeAnimationPop]; // 角标+count [(HHCountBadge *)self.oneView.hh_badge increaseBy:(NSInteger)count animationWithType:HHBadgeAnimationPop]; // 角标-1 [(HHCountBadge *)self.oneView.hh_badge decreaseWithAnimationType:HHBadgeAnimationPop]; // 角标-count [(HHCountBadge *)self.oneView.hh_badge decreaseBy:(NSInteger)count animationWithType:HHBadgeAnimationPop]; // 2. 给UIBarButtonItem添加通知图标 self.item.hh_badge = [HHCountBadge badgeWithCount:99]; 使用方法和上述一样, 有一点需要注意,给item设置此属性时需保证此时item的view属性已被加载,也即不为nil。</code></pre> <p>###点图标样式</p> <pre> <code class="language-objectivec">// 1. 给UIView添加通知图标 self.oneView.hh_badge = [HHCountBadge badge]; // 配置外观样式 [(HHDotBadge *)self.oneView.hh_badge applyStyle:^(HHDotBadgeStyle *style) { style.bgColor = [UIColor cyanColor]; style.borderColor = [UIColor redColor]; style.borderWidth = 1; ... }]; // 动画 [(HHDotBadge *)self.oneView.hh_badge doAnimationWithType:self.type]; // 向左移5, 向下移5 [(HHDotBadge *)self.oneView.hh_badge moveByX:-5 Y:5]; // 2. 给UIBarButtonItem添加通知图标 self.item.hh_badge = [HHCountBadge badge]; 使用方法及注意点同上</code></pre> <h2>关键类定义</h2> <p>###支持动画类型</p> <pre> <code class="language-objectivec">typedef NS_ENUM(NSUInteger, HHBadgeAnimationType) { HHBadgeAnimationPop = 0,///< 缩放动画 HHBadgeAnimationBlink,///< 眨眼动画 HHBadgeAnimationBump,///< 上下跳动动画 HHBadgeAnimationNone///< 无 };</code></pre> <p>###点图标样式模型HHDotBadgeStyle</p> <pre> <code class="language-objectivec">@interface HHDotBadgeStyle : NSObject /// 直径 defalut 15 @property (nonatomic, assign)CGFloat badgeDiameter; /// 圆圈的背景颜色 @property (nonatomic, strong)UIColor *bgColor; /// 边框颜色 @property (nonatomic, strong)UIColor *borderColor; /// 边框宽度 @property (nonatomic, assign)CGFloat borderWidth; /// 是否支持QQ的粘性拖拽效果 default NO 此功能暂时未实现 后继版本加入 //@property (nonatomic, assign, getter=haveGooEffect)BOOL gooEffect; @end</code></pre> <p>###数字图标样式模型HHCountBadgeStyle</p> <pre> <code class="language-objectivec">@interface HHCountBadgeStyle : HHDotBadgeStyle /// 是否自适应大小 default YES @property (nonatomic, assign)BOOL automaticExpansion; /// 是否强制显示0 default NO @property (nonatomic, assign, getter=shouldForcedShowZero)BOOL forceShowZero; /// 数字的颜色 @property (nonatomic, strong)UIColor *countColor; /// 数字的字体 @property (nonatomic, strong)UIFont *countFont; /// 数字的对齐方式 @property (nonatomic, assign)NSTextAlignment countAlignment; @end</code></pre> <p>###通知图标基类HHBadge</p> <pre> <code class="language-objectivec">@interface HHBadge : NSObject { __weak UIView *_sourceView; __weak UIBarButtonItem *_sourceItem; } /// 快速创建实例 + (instancetype)badge; - (void)show; - (void)hide; /// 源视图 @property (nonatomic, weak, readonly)UIView *sourceView; /// 源item @property (nonatomic, weak, readonly)UIBarButtonItem *sourceItem; @end</code></pre> <p>###点样式通知图标HHDotBadge</p> <pre> <code class="language-objectivec">@interface HHDotBadge : HHBadge /// 配置外观样式 - (void)applyStyle:(void (^)(HHDotBadgeStyle *style))maker; /// 配置位置属性 - (void)moveByX:(CGFloat)x Y:(CGFloat)y; - (void)scaleBy:(CGFloat)scale; - (void)doAnimationWithType:(HHBadgeAnimationType)type; @end</code></pre> <p>###数字样式通知图标HHCountBadge</p> <pre> <code class="language-objectivec">@interface HHCountBadge : HHDotBadge /// 快速创建实例 + (instancetype)badgeWithCount:(NSInteger)count; - (instancetype)initWithCount:(NSInteger)count NS_DESIGNATED_INITIALIZER; /// 配置外观样式 - (void)applyStyle:(void (^)(HHCountBadgeStyle *style))maker; - (void)increaseWithAnimationType:(HHBadgeAnimationType)type; - (void)increaseBy:(NSInteger)count animationWithType:(HHBadgeAnimationType)type; - (void)decreaseWithAnimationType:(HHBadgeAnimationType)type; - (void)decreaseBy:(NSInteger)count animationWithType:(HHBadgeAnimationType)type; /// 数量 @property (nonatomic, assign)NSInteger count; @end</code></pre> <p>###分类</p> <pre> <code class="language-objectivec">@interface UIView (HHBadgeHUD) @property (nonatomic, strong) __kindof HHBadge *hh_badge; @end @interface UIBarButtonItem (HHBadgeHUD) /// 设置此值的时候确保Item的View属性已被加载 @property (nonatomic, strong) __kindof HHBadge *hh_badge; @end</code></pre> <h2>安装</h2> <h3>CocoaPods</h3> <ol> <li>在 Podfile 中添加 pod 'HHBadgeHUD' 。</li> <li>执行 pod install 或 pod update 。</li> <li>导入 <HHBadgeHUD/HHBadgeHUD.h>。</li> </ol> <h3>手动安装</h3> <ol> <li>下载 HHBadgeHUD 文件夹内的所有内容。</li> <li>将 HHBadgeHUD 内的源文件添加(拖放)到你的工程。</li> <li>导入 HHBadgeHUD.h 。</li> </ol> <h2>系统要求</h2> <p>该项目最低支持 iOS 6.0 和 Xcode 7.0 。</p> <h2>许可证</h2> <p>HHBadgeHUD 使用 MIT 许可证,详情见 LICENSE 文件。</p> <p> </p> <p>项目主页:<a href="http://www.open-open.com/lib/view/home/1495596301102">http://www.open-open.com/lib/view/home/1495596301102</a></p> <p> </p>