LabeledActivityIndicatorView
UIActivityIndicatorView with a label which can be added to UIButton and UINavigationBar.UINavigationItem
先上一張微信聊天界面導航欄表示網(wǎng)絡請求的效果圖
Wechat
再放一張最終的效果圖
UIActivityIndicatorView
以下要做的就是給UIActivityIndicatorView
添加一個注釋兔辅,來實現(xiàn)微信NavigationBar
上的效果,不多說,擼代碼:
-
LabeledActivityIndicatorView.h文件
@interface LabeledActivityIndicatorView : UIView /** 指定activityIndicatorView的style的初始化方式版述,也可直接[LabeledActivityIndicatorView new]創(chuàng)建 @param style 只有 UIActivityIndicatorViewStyleWhite或者 UIActivityIndicatorViewStyleGray 可選广料,默認為UIActivityIndicatorViewStyleGray */ - (instancetype)initWithActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)style; /** 設置小菊花后邊的文字 @param description 需要顯示的文字 @param font 字體大小樣式(為nil時默認為第一次設置的大小钦扭,未設置過則為默認的17號字體) @param color 字體顏色(為nil時默認為第一次設置的顏色戒劫,未設置過則為黑色) */ - (void)setDescription:(NSString *)description font:(UIFont *)font color:(UIColor *)color; /** 開始旋轉(zhuǎn) */ - (void)startRotation; /** 停止旋轉(zhuǎn)并隱藏小菊花 */ - (void)stopRotation; /** 停止旋轉(zhuǎn)嗅骄,隱藏小菊花并顯示完成圖標“success.png” */ - (void)stopRotationWithDone; /** 將該view移除胳挎,如果父視圖為button,則自動恢復button的原title */ - (void)stopRotationWithFaild; @end
-
LabeledActivityIndicatorView.m
@interface LabeledActivityIndicatorView() @property (nonatomic, strong) UIActivityIndicatorView *aiView; @property (nonatomic, strong) UILabel *label; @property (nonatomic, assign) CGFloat labelWidth; @property (nonatomic, weak) UIImageView *suc; @property (nonatomic, strong) NSString *buttonTitle; @end @implementation LabeledActivityIndicatorView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self addSubViewsWithStyle:UIActivityIndicatorViewStyleGray]; } return self; } - (instancetype)initWithActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)style { if (self = [super init]) { [self addSubViewsWithStyle:style]; } return self; } - (void)addSubViewsWithStyle:(UIActivityIndicatorViewStyle)style { self.userInteractionEnabled = NO; if (style == UIActivityIndicatorViewStyleWhiteLarge) style = UIActivityIndicatorViewStyleGray; _aiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style]; _aiView.frame = CGRectMake(0, 0, 20, 20); _aiView.hidesWhenStopped = YES; [self addSubview:_aiView]; _label = [UILabel new]; _label.backgroundColor = [UIColor clearColor]; [self addSubview:_label]; } - (void)setDescription:(NSString *)description font:(UIFont *)font color:(UIColor *)color { if ([self.superview isKindOfClass:[UIButton class]]) { self.buttonTitle = [((UIButton *)(self.superview)).titleLabel.text copy]; [((UIButton *)(self.superview)) setTitle:nil forState:UIControlStateNormal]; } // else if ([self.superview isKindOfClass:[UINavigationBar class]]) { // self.buttonTitle = ((UINavigationBar *)(self.superview)).items.firstObject.title; // } // 暫時不適配導航欄 需要手動回復title _label.text = description; if (font) _label.font = font; if (color) _label.textColor = color; [self updateLayout]; } - (void)updateLayout{ CGFloat superViewWidth = self.superview.frame.size.width; CGRect rect = self.frame; rect.size.height = _aiView.frame.size.height; CGFloat aiViewWidth = _aiView.hidden ? 0 : _aiView.frame.size.width + 3; CGFloat sucWidth = _suc ? 23 : 0; rect.size.width = aiViewWidth + self.labelWidth + sucWidth; if (rect.size.width > superViewWidth && superViewWidth > 0) { rect.size.width = superViewWidth; } self.frame = rect; self.center = CGPointMake(superViewWidth * 0.5, self.superview.frame.size.height * 0.5); _label.frame = CGRectMake(aiViewWidth + sucWidth, 0, rect.size.width - aiViewWidth - sucWidth, _aiView.frame.size.height); [self setNeedsLayout]; } - (CGFloat)labelWidth { NSString *str = _label.text; NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = _label.font; CGFloat width = [str boundingRectWithSize:CGSizeMake(0, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width; return width; } - (void)startRotation { _aiView.hidden = NO; [_suc removeFromSuperview]; _suc = nil; [self.aiView startAnimating]; [self updateLayout]; } - (void)stopRotation { [_aiView stopAnimating]; [self updateLayout]; } - (void)stopRotationWithDone { [_suc removeFromSuperview]; UIImageView *suc = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; suc.image = [UIImage imageNamed:@"success.png"]; self.aiView.hidden = YES; [self addSubview:suc]; _suc = suc; [self updateLayout]; } - (void)stopRotationWithFaild { if ([self.superview isKindOfClass:[UIButton class]]) { [(UIButton *)self.superview setTitle:self.buttonTitle forState:UIControlStateNormal]; } // else if ([self.superview isKindOfClass:[UINavigationBar class]]) { // ((UINavigationBar *)(self.superview)).items.firstObject.title = self.buttonTitle; // } // 暫時不適配導航欄 [self removeFromSuperview]; } @end
最后放上GitHub鏈接Camoufleur掸读,喜歡的給個Star吧.