imitate wechat - 4

来源:互联网 发布:梵高奶奶 知乎 编辑:程序博客网 时间:2024/06/10 12:51

接上篇:

View层的实现:

三:

ChartCell.h

#import <UIKit/UIKit.h>@class ChartCell;@protocol ChartCellDelegate <NSObject>- (void)chartCell:(ChartCell *)chartCell tapContent:(NSString *)content;@end#import "ChartCellFrame.h"@interface ChartCell : UITableViewCell@property (nonatomic, strong)ChartCellFrame* cellFrame;@property (nonatomic, assign)id<ChartCellDelegate> delegate;@end

ChartCell.m

#import "ChartCell.h"#import "ChartContentView.h"@interface ChartCell ()<ChartContentViewDelegate>@property (nonatomic, strong) UIImageView* icon;@property (nonatomic, strong) ChartContentView* chartView;@property (nonatomic, strong) ChartContentView* currentChartView;@property (nonatomic, strong) NSString* contentStr;@end@implementation ChartCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];        if (self)    {        self.backgroundColor = [UIColor clearColor];                self.icon = [[UIImageView alloc] init];        [self.contentView addSubview:self.icon];                self.chartView = [[ChartContentView alloc] initWithFrame:CGRectZero];        self.chartView.delegate = self;        [self.contentView addSubview:self.chartView];    }        return self;}- (void)setCellFrame:(ChartCellFrame *)cellFrame{    _cellFrame = cellFrame;        ChartMessage* chartMessage = cellFrame.chartMessage;        self.icon.frame = cellFrame.iconRect;    self.icon.image = [UIImage imageNamed:(NSString*)chartMessage.icon];        self.chartView.chartMessage = chartMessage;    self.chartView.frame = cellFrame.chartViewRect;        [self setBackGroundImageViewImage:self.chartView from:@"chatfrom_bg_normal.png" to:@"chatto_bg_normal.png"];        self.chartView.contentLabel.text = chartMessage.content;    }- (void)setBackGroundImageViewImage:(ChartContentView *)chartView from:(NSString *)from to:(NSString *)to{    UIImage* normal = nil;        if (chartView.chartMessage.messageType == kMessageFrom)    {        normal = [UIImage imageNamed:from];        normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5                                             topCapHeight:normal.size.height * 0.7];    }    else if (chartView.chartMessage.messageType == kMessageTo)    {        normal = [UIImage imageNamed:to];        normal = [normal stretchableImageWithLeftCapWidth:normal.size.width * 0.5                                             topCapHeight:normal.size.height * 0.7];    }        chartView.backImageView.image = normal;}#pragma mark - 委托方法LongPress && TapPress- (void)chartContentViewLongPress:(ChartContentView *)chartView content:(NSString *)content{    [self becomeFirstResponder];    UIMenuController* menu = [UIMenuController sharedMenuController];    [menu setTargetRect:self.bounds inView:self];    [menu setMenuVisible:YES animated:YES];        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuShow:) name:UIMenuControllerWillShowMenuNotification object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuHide:) name:UIMenuControllerWillHideMenuNotification object:nil];            self.contentStr = content;        self.currentChartView = chartView;}- (void)chartContentViewTapPress:(ChartContentView *)chartView content:(NSString *)content{    if ([self.delegate respondsToSelector:@selector(chartCell:tapContent:)])    {        [self.delegate chartCell:self tapContent:content];    }}- (void)menuShow:(UIMenuController *)menu{    NSLog(@" menu Show Operation : - !");    [self setBackGroundImageViewImage:self.currentChartView from:@"chatfrom_bg_focused.png" to:@"chatto_bg_focused.png"];}- (void)menuHide:(UIMenuController *)menu{    [self setBackGroundImageViewImage:self.currentChartView from:@"chatfrom_bg_normal.png" to:@"chatto_bg_normal.png"];        self.currentChartView=nil;    [self resignFirstResponder];}- (BOOL)canBecomeFirstResponder{    return YES;}- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{    if(action ==@selector(copy:))    {        return YES;    }        return [super canPerformAction:action withSender:sender];}- (void)copy:(id)sender{    [[UIPasteboard generalPasteboard]setString:self.contentStr];}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{    [super setSelected:selected animated:animated];}@end


Controller层的实现:

主要在于调用M和V层!

@interface LBWeChatTKViewController () <KeyBordVIewDelegate, ChartCellDelegate,UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>@property (nonatomic,strong) NSMutableArray *cellFrames; /** 抽取的数据 **/@endstatic NSString *const cellIdentifier=@"QQChart";@implementation LBWeChatTKViewController- (void)viewDidLoad{    [super viewDidLoad];    /** 省略的代码 **/    [self initwithData];}- (void)initwithData{    self.cellFrames = [NSMutableArray array];}/**  * 省略的很多方法实现 */- (void)KeyBordView:(KeyBordView *)keyBoardView textFiledReturn:(UITextField *)textFiled{        //这个也是数据的滚滚由来    ChartCellFrame* cellFrame  = [[ChartCellFrame alloc]init];    ChartMessage* chartMessage = [[ChartMessage alloc]init];    int random = arc4random_uniform(2);        NSLog(@"%d",random);        if (random == 0)    {        chartMessage.icon = @"0.jpg";    }    else if(random == 1)    {        chartMessage.icon = @"12.jpg";    }        chartMessage.messageType = random;    chartMessage.content     = textFiled.text;    cellFrame.chartMessage   = chartMessage;        [self.cellFrames addObject:cellFrame];    [self.tableView reloadData];        //再置空    textFiled.text = @"";}#pragma mark - UITableViewDelegate.- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.cellFrames.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return [self.cellFrames[indexPath.row] cellHeight];}- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    ChartCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];        cell.delegate = self;    cell.cellFrame = self.cellFrames[indexPath.row];        return cell;}@end

效果图如下:



下篇接下来是相关的解析!


0 0