IOS開源:HHBadgeHUD-一款更 Q 的通知圖標控件(OC)
簡介
一款更Q的通知圖標控件。
演示項目
查看并運行 HHBadgeHUDDemo/HHBadgeHUDDemo.xcodeproj
特性
- 無侵入性 :
- 輕量 :
- 易擴展 :
使用方法
###數字圖標樣式
// 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>
###點圖標樣式
// 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>
關鍵類定義
###支持動畫類型
typedef NS_ENUM(NSUInteger, HHBadgeAnimationType) {
HHBadgeAnimationPop = 0,///< 縮放動畫
HHBadgeAnimationBlink,///< 眨眼動畫
HHBadgeAnimationBump,///< 上下跳動動畫
HHBadgeAnimationNone///< 無
};
###點圖標樣式模型HHDotBadgeStyle
@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>
###數字圖標樣式模型HHCountBadgeStyle
@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>
###通知圖標基類HHBadge
@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>
###點樣式通知圖標HHDotBadge
@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>
###數字樣式通知圖標HHCountBadge
@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>
###分類
@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>
安裝
CocoaPods
- 在 Podfile 中添加 pod 'HHBadgeHUD' 。
- 執行 pod install 或 pod update 。
- 導入 <HHBadgeHUD/HHBadgeHUD.h>。
手動安裝
- 下載 HHBadgeHUD 文件夾內的所有內容。
- 將 HHBadgeHUD 內的源文件添加(拖放)到你的工程。
- 導入 HHBadgeHUD.h 。
系統要求
該項目最低支持 iOS 6.0 和 Xcode 7.0 。
許可證
HHBadgeHUD 使用 MIT 許可證,詳情見 LICENSE 文件。
項目主頁:http://www.baiduhome.net/lib/view/home/1495596301102