全平台头像添加vip标识-视图封装思想
Posted huangzs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全平台头像添加vip标识-视图封装思想相关的知识,希望对你有一定的参考价值。
需求:全平台(整个APP)头像不同的vip级别添加不同的vip标识
样式:(圆形头像右下角添加vip标识)
为了便于调用,采用了 UIImageView 分类,添加方法,方法里添加一个 UIImageView 展示vip标识 的实现方式。直接上代码
1.新建 UIImageView+TJVipHead 分类
2..h里代码
@interface UIImageView (TJVipHead) @property(strong, nonatomic) UIImageView *vipMarkImageView;
/// VIP 标识宽度 默认 Ratio(16)*Ratio(16) /// @param vipLevel vip级别 /// @param vipStatus VIP 状态 -(void)addVipMarkWithVipLevel:(NSString *)vipLevel vipStatus:(NSString *)vipStatus;
@end |
3..m里代码
@implementation UIImageView (TJVipHead)
-(void)addVipMarkWithVipLevel:(NSString *)vipLevel vipStatus:(NSString *)vipStatus { if (![self.superview.subviews containsObject:self.vipMarkImageView]) { self.vipMarkImageView = [[UIImageView alloc] init]; self.vipMarkImageView.size=CGSizeMake(Ratio(16), Ratio(16)); self.vipMarkImageView.right=self.right; self.vipMarkImageView.bottom=self.bottom; self.vipMarkImageView.backgroundColor = [UIColor clearColor]; self.vipMarkImageView.userInteractionEnabled=YES; self.vipMarkImageView.image=nil; [self.superview addSubview:self.vipMarkImageView]; } [self.superview bringSubviewToFront:self.vipMarkImageView]; if (beNil(vipLevel)) { vipLevel=@"0"; } if (beNil(vipStatus)) { vipStatus=@"0"; } if (vipStatus.integerValue) { //vip有效 //vipLevel//会员级别 1:普通;2:高级;3:特级; switch (vipLevel.integerValue) { case 0: self.vipMarkImageView.image=nil; break; case 1: self.vipMarkImageView.image=[UIImage imageNamed:@"common_vip"]; break; case 2: self.vipMarkImageView.image=[UIImage imageNamed:@"common_vip_senior"]; break; case 3: self.vipMarkImageView.image=[UIImage imageNamed:@"common_vip_super"]; break; default: break; } } else { //vip失效或者没开通 self.vipMarkImageView.image=nil; } }
#pragma mark - Add a "vipMarkImageView" to this UIImageView class
NSString const *kKeyVipMarkImageView = @"kKeyVipMarkImageView"; -(void)setVipMarkImageView:(UIImageView *)vipMarkImageView { objc_setAssociatedObject(self, &kKeyVipMarkImageView, (id)vipMarkImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(UIImageView *)vipMarkImageView { return objc_getAssociatedObject(self, &kKeyVipMarkImageView); }
@end |
以上是关于全平台头像添加vip标识-视图封装思想的主要内容,如果未能解决你的问题,请参考以下文章