iOS开发项目篇—44会员处理
iOS开发项目篇—44会员处理
一、简单说明
查看已经完成的效果:
存在的问题和调整:
1 // 2 // YYHttpTool.m 3 // 4 5 #import "YYHttpTool.h" 6 #import "AFNetworking.h" 7 @implementation YYHttpTool 8 +(void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure 9 { 10 //1.获得请求管理者 11 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; 12 13 //3.发送Get请求 14 [mgr GET:url parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary*responseObj) { 15 if (success) { 16 success(responseObj); 17 YYLog(@"%@",responseObj); 18 } 19 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 20 if (failure) { 21 failure(error); 22 } 23 }]; 24 }
json数据(截取了返回的用户信息):
与会员相关的属性:
mbrank:会员等级
1 // 2 // YYUserModel.h 3 // 4 5 #import <Foundation/Foundation.h> 6 7 @interface YYUserModel : NSObject 8 9 /** string 友好显示名称*/ 10 @property(nonatomic,copy)NSString *name; 11 12 /**string 用户头像地址(中图),50×50像素*/ 13 @property(nonatomic,copy)NSString *profile_image_url; 14 15 /**用户的会员等级*/ 16 @property(nonatomic,assign)int mbrank; 17 18 /**用户的会员类型*/ 19 @property(nonatomic,assign)int mbtype; 20 21 /**是否为会员*/ 22 @property(nonatomic,assign,getter = isVip)BOOL vip; 23 24 //接口 25 //+(instancetype)userModelWithDict:(NSDictionary *)Dict; 26 @end
YYUserModel.m文件
1 // 2 // YYUserModel.m 3 // 4 5 #import "YYUserModel.h" 6 7 @implementation YYUserModel 8 //+(instancetype)userModelWithDict:(NSDictionary *)Dict 9 //{ 10 // YYUserModel *model=[[self alloc]init]; 11 // model.name=Dict[@"name"]; 12 // model.profile_image_url=Dict[@"profile_image_url"]; 13 // return model; 14 //} 15 16 -(BOOL)isVip 17 { 18 //如果大于2,那么就是会员 19 return self.mbtype > 2; 20 } 21 @end
(2)在原创微博的View上添加会员图标
YYStatusOriginal.m文件
1 // 2 // YYStatusOriginal.m 3 // 4 5 #import "YYStatusOriginalView.h" 6 #import "YYStatusOriginalFrame.h" 7 #import "YYUserModel.h" 8 #include "YYStatusModel.h" 9 #import "UIImageView+WebCache.h" 10 11 @interface YYStatusOriginalView () 12 /** 13 * 头像 14 */ 15 @property(nonatomic,weak)UIImageView *iconView; 16 /** 17 * 昵称 18 */ 19 @property(nonatomic,weak)UILabel *nameLabel; 20 /** 21 * 微博的正文 22 */ 23 @property(nonatomic,weak)UILabel *textLabel; 24 /** 25 * 时间 26 */ 27 @property(nonatomic,weak)UILabel *timeLabel; 28 /** 29 * 来源 30 */ 31 @property(nonatomic,weak)UILabel *sourceLabel; 32 /** 33 * 会员图标 34 */ 35 @property(nonatomic,weak)UIImageView *vipView; 36 37 @end 38 @implementation YYStatusOriginalView 39 40 - (id)initWithFrame:(CGRect)frame 41 { 42 self = [super initWithFrame:frame]; 43 if (self) { 44 //初始化子控件 45 //1.头像 46 UIImageView *iconView=[[UIImageView alloc]init]; 47 [self addSubview:iconView]; 48 self.iconView=iconView; 49 50 //2.昵称 51 UILabel *nameLabel=[[UILabel alloc]init]; 52 nameLabel.font=YYStatusOrginalNameFont; 53 [self addSubview:nameLabel]; 54 self.nameLabel=nameLabel; 55 56 //3.正文 57 UILabel *textLabel=[[UILabel alloc]init]; 58 textLabel.font=YYStatusOrginalTextFont; 59 textLabel.numberOfLines=0; 60 [self addSubview:textLabel]; 61 self.textLabel=textLabel; 62 63 //4.时间 64 UILabel *timeLabel=[[UILabel alloc]init]; 65 timeLabel.font=YYStatusOrginalTimeFont; 66 timeLabel.textColor=[UIColor orangeColor]; 67 [self addSubview:timeLabel]; 68 self.timeLabel=timeLabel; 69 70 //5.来源 71 UILabel *sourceLabel=[[UILabel alloc]init]; 72 sourceLabel.font=YYStatusOrginalSourceFont; 73 sourceLabel.textColor=[UIColor lightGrayColor]; 74 [self addSubview:sourceLabel]; 75 self.sourceLabel=sourceLabel; 76 77 //6.会员图标 78 UIImageView *vipView=[[UIImageView alloc]init]; 79 //会员图标应该设置保持原来的尺寸,垂直居中 80 vipView.contentMode=UIViewContentModeCenter; 81 [self addSubview:vipView]; 82 self.vipView=vipView; 83 84 } 85 return self; 86 } 87 88 -(void)setOriginalFrame:(YYStatusOriginalFrame *)originalFrame 89 { 90 _originalFrame=originalFrame; 91 92 //设置自己的frame 93 self.frame=originalFrame.frame; 94 95 //取出模型数据 96 YYStatusModel *status=originalFrame.status; 97 98 //设置头像的frame 99 [self.iconView setImageWithURL:[NSURL URLWithString:status.user.profile_image_url] placeholderImage:[UIImage imageWithName:@"avatar_default_small"]]; 100 self.iconView.frame=originalFrame.iconFrame; 101 102 //设置昵称的frame 103 self.nameLabel.text=status.user.name; 104 //注意循环利用的问题 105 if (status.user.isVip) { 106 self.nameLabel.textColor=[UIColor orangeColor]; 107 self.vipView.hidden=NO; 108 self.vipView.frame=originalFrame.vipViewFrame; 109 self.vipView.image=[UIImage imageWithName:[NSString stringWithFormat:@"common_icon_membership_level%d",status.user.mbrank]]; 110 }else 111 { 112 self.nameLabel.textColor=[UIColor blackColor]; 113 self.vipView.hidden=YES; 114 } 115 self.nameLabel.frame=originalFrame.nameFrame; 116 117 //设置正文的frame 118 self.textLabel.text=status.text; 119 self.textLabel.frame=originalFrame.textFrame; 120 121 //设置时间的frame 122 self.timeLabel.text=status.created_at; 123 self.timeLabel.frame=originalFrame.timeFrame; 124 125 //设置来源的frame 126 self.sourceLabel.text=status.source; 127 self.sourceLabel.frame=originalFrame.sourceFrame; 128 129 130 } 131 @end
frame模型中的代码设计:
YYStatusOriginalFrame.m文件
1 // 2 // YYStatusOriginalFrame.m 3 // 4 5 #import "YYStatusOriginalFrame.h" 6 #import "YYStatusModel.h" 7 #import "YYUserModel.h" 8 9 10 @implementation YYStatusOriginalFrame 11 -(void)setStatus:(YYStatusModel *)status 12 { 13 _status=status; 14 //1.计算头像的frame 15 CGFloat iconX=YYCellStatusInset; 16 CGFloat iconY=YYCellStatusInset; 17 CGFloat iconW=35; 18 CGFloat iconH=35; 19 self.iconFrame=CGRectMake(iconX, iconY, iconW, iconH); 20 21 //2.计算昵称的frame 22 CGFloat nameX=CGRectGetMaxX(self.iconFrame) + YYCellStatusInset; 23 CGFloat nameY=iconY; 24 CGSize nameSize=[status.user.name sizeWithFont:YYStatusOrginalNameFont]; 25 self.nameFrame=(CGRect){{nameX,nameY},nameSize}; 26 27 //3.计算时间的frame 28 CGFloat timeX=nameX; 29 CGFloat timeY=CGRectGetMaxY(self.nameFrame)+YYCellStatusInset*0.5; 30 CGSize timeSize=[status.created_at sizeWithFont:YYStatusOrginalTimeFont]; 31 self.timeFrame=(CGRect){{timeX,timeY},timeSize}; 32 33 //4.计算发布来源的frame 34 CGFloat sourcceX=CGRectGetMaxX(self.timeFrame)+YYCellStatusInset; 35 // CGFloat sourcceX=CGRectGetMaxX(self.textFrame)+YYCellStatusInset; 36 CGFloat sourcceY=timeY; 37 CGSize sourcceSize=[status.source sizeWithFont:YYStatusOrginalSourceFont]; 38 self.sourceFrame=(CGRect){{sourcceX,sourcceY},sourcceSize}; 39 40 //5.计算微博正文的frame 41 #warning 注意计算的区别 42 CGFloat textX=iconX; 43 CGFloat textY=CGRectGetMaxY(self.iconFrame)+YYCellStatusInset; 44 CGFloat maxW=YYScreenW-2*YYCellStatusInset; 45 CGSize maxSize=CGSizeMake(maxW, MAXFLOAT); 46 CGSize textSize=[status.text sizeWithFont:YYStatusOrginalTextFont constrainedToSize:maxSize]; 47 self.textFrame=(CGRect){{textX,textY},textSize}; 48 49 //6.计算会员图标的frame 50 //如果该用户是vip,那么就计算会员图标 51 if (status.user.isVip) { 52 // CGFloat vipX=CGRectGetMaxX(self.nameFrame) + YYCellStatusInset; 53 CGFloat vipX=CGRectGetMaxX(self.nameFrame) + YYCellStatusInset; 54 CGFloat vipY=nameY; 55 CGFloat vipH=nameSize.height; 56 CGFloat vipW=vipH; 57 self.vipViewFrame=CGRectMake(vipX, vipY, vipW, vipH); 58 } 59 60 //7.计算自己的frame 61 CGFloat x=0; 62 CGFloat y=0; 63 CGFloat w=YYScreenW; 64 // CGFloat h=200; 65 CGFloat h=CGRectGetMaxY(self.textFrame)+YYCellStatusInset; 66 self.frame=CGRectMake(x, y, w, h); 67 } 68 @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。