day 029 QQ实现FaceView及输入框输入

//
//  FaceView.h
//  QQ
//
//  Created by PXD on 15-5-3.
//  Copyright (c) 2015ๅนด PXD. All rights reserved.
//


#import <UIKit/UIKit.h>
@protocol FaceViewDelegate <NSObject>
- (void)faceDidClickedWithName:(NSString *)faceName;
@end


@interface FaceView : UIView


@property (nonatomic, assign)id<FaceViewDelegate> delegate;


@end


//
//  FaceView.m
//  QQ
//
//  Created by PXD on 15-5-3.
//  Copyright (c) 2015ๅนด PXD. All rights reserved.
//


#import "FaceView.h"
#import "UIScrollView+Prevent.h"


//่ฎก็ฎ—้—ด่ท  ๏ผˆ320 ๏ผ 7๏ผŠ30 ๏ผ‰๏ผ8 ๏ผ 14
#define kFaceWidthPadding 14


// (253 - 4 * 30) / 5 = 26.6
#define kfaceHeightPadding 25.6


@interface FaceView ()
@property (nonatomic, strong) UIScrollView *faceScrollView;
@property (nonatomic, strong) NSArray *faceArray;
@property (nonatomic, strong) UIImageView *bigFaceImageView;
@property (nonatomic, strong) UIImageView *faceImageView;
@property (nonatomic, assign) int index;
@end


@implementation FaceView


/*
 7 * 4 = 28
 108 / 28 = 4
 
 */
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.faceScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _faceScrollView.backgroundColor = [UIColor lightGrayColor];
        _faceScrollView.pagingEnabled = YES;
        [self addSubview:_faceScrollView];
        
        
        //ๅฐ†่กจๆƒ…ๆทปๅŠ ๅˆฐๆปšๅŠจ่ง†ๅ›พไธŠ
        //่ฏปๅ–plistๆ–‡ไปถ
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"];
        self.faceArray = [NSArray arrayWithContentsOfFile:filePath];
        
        //ไฝฟ็”จforๅพช็Žฏไพๆฌกๅฐ†ๆ‰€ๆœ‰่กจๆƒ…ๆทปๅŠ ๅˆฐscrollview
        for (int page = 0; page < 4; page++) {//ๆŽงๅˆถ้กต
            CGFloat startX = kFaceWidthPadding + page*320;
            //104 / 28 = 3 ...20
            //104 - 28*3 = 20 / 7 = 2
            int maxRow =(_faceArray.count - page*28) > 28 ? 4 : ((_faceArray.count - page*28) % 7 == 0) ? (_faceArray.count - page*28)/7 : (_faceArray.count - page*28)/7 + 1;
            for (int row = 0; row < maxRow; row++) {//ๆŽงๅˆถ่กŒ
                CGFloat startY = kfaceHeightPadding + row * (30 + kfaceHeightPadding);
                
                int maxColum = (int)(_faceArray.count - page*28 - row*7) > 7 ? 7 : _faceArray.count - page*28 - row*7;
                
                for (int colum = 0; colum < maxColum; colum++) {//ๆŽงๅˆถๅˆ—
                    startX = kFaceWidthPadding +  colum * (30 + kFaceWidthPadding) + page*320;
                    
                    //ๅพ—ๅˆฐ่ฟ™ไธช่กจๆƒ…ๅ›พ็‰‡
                    UIImage *faceImage = [UIImage imageNamed:[[_faceArray objectAtIndex:28*page + row*7 + colum] objectForKey:@"png"]];
                    
                    //่ฎก็ฎ—่กจๆƒ…่ง†ๅ›พ็š„frame
                    CGRect faceFrame = CGRectMake(startX, startY, 30, 30);
                    
                    
                    //ๅˆ›ๅปบUIImageViewๆ˜พ็คบๅ›พ็‰‡
                    UIImageView *faceImageView = [[UIImageView alloc] initWithFrame:faceFrame];
                    faceImageView.image = faceImage;
                    
                    //ๆทปๅŠ ๅˆฐscrollVIew
                    [self.faceScrollView addSubview:faceImageView];
                }
            }
        }
        
        //contentSize
        self.faceScrollView.contentSize = CGSizeMake(4 * 320, 253);
    }
    return self;
}


- (UIImageView *)bigFaceImageView{
    if (_bigFaceImageView == nil) {
        _bigFaceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 64, 92)];
        _bigFaceImageView.image = [UIImage imageNamed:@"emoticon_keyboard_magnifier"];
        [self addSubview:_bigFaceImageView];
        
        self.faceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, 12, 40, 40)];
        _faceImageView.image = [UIImage imageNamed:@"001.png"];
        [_bigFaceImageView addSubview:_faceImageView];
    }
    _bigFaceImageView.hidden = NO;
    return _bigFaceImageView;
}
#pragma mark --- UITouchEvent
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //ๅ…ณ้—ญscrollView็š„ๆปšๅŠจๅŠŸ่ƒฝ
    _faceScrollView.scrollEnabled = NO;
    
    self.bigFaceImageView.backgroundColor = [UIColor clearColor];
    
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    
    //่Žทๅ–ๅฝ“ๅ‰้กตๆ•ฐ
    int page = _faceScrollView.contentOffset.x / 320;
    
    int row = 0;
    int colum = 0;
    
    //่ฎก็ฎ—่กŒ
    int rowAvgHeigth = 253 / 4;
    row = location.y / rowAvgHeigth;
    
    //่ฎก็ฎ—ๅˆ—
    int columAvgWidth = 320 / 7;
    colum = location.x / columAvgWidth;
  
    //่ฎก็ฎ—ๅ‡บ็‚นๅ‡ป็š„ๆ˜ฏ็ฌฌๅ‡ ไธช
    
    //่ฎก็ฎ—ๅ‡บ็‚นๅ‡ป็š„ๆ˜ฏ็ฌฌๅ‡ ไธช
    _index = page*28 + row * 7 + colum;
    
    //่Žทๅ–่ฟ™ๅผ ๅ›พ็‰‡
    UIImage *faceImage = [UIImage imageNamed:[[_faceArray objectAtIndex:_index] objectForKey:@"png"]];
    
    //ๆ”พๅˆฐๆ”พๅคง้•œไธŠ้ข
    self.faceImageView.image = faceImage;
    
    self.bigFaceImageView.frame = CGRectMake(colum * columAvgWidth + columAvgWidth/2 - 29, row * rowAvgHeigth + rowAvgHeigth / 2 - 92, 64, 92);
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    
    //่Žทๅ–ๅฝ“ๅ‰้กตๆ•ฐ
    int page = _faceScrollView.contentOffset.x / 320;
    
    int row = 0;
    int colum = 0;
    
    //่ฎก็ฎ—่กŒ
    int rowAvgHeigth = (253 - kfaceHeightPadding/2) / 4;
    row = location.y / rowAvgHeigth;
    
    //่ฎก็ฎ—ๅˆ—
    int columAvgWidth = 320 / 7;
    colum = location.x / columAvgWidth;
    
    //่ฎก็ฎ—ๅ‡บ็‚นๅ‡ป็š„ๆ˜ฏ็ฌฌๅ‡ ไธช
    _index = page*28 + row * 7 + colum;
    
    //่Žทๅ–่ฟ™ๅผ ๅ›พ็‰‡
    UIImage *faceImage = [UIImage imageNamed:[[_faceArray objectAtIndex:_index] objectForKey:@"png"]];
    
    //ๆ”พๅˆฐๆ”พๅคง้•œไธŠ้ข
    self.faceImageView.image = faceImage;
    
    self.bigFaceImageView.frame = CGRectMake(colum * columAvgWidth + columAvgWidth/2 - 29, row * rowAvgHeigth + rowAvgHeigth / 2 - 92, 64, 92);
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //ๆ‰“ๅผ€ๆปšๅŠจๅŠŸ่ƒฝ
    _faceScrollView.scrollEnabled = YES;
    
    _bigFaceImageView.hidden = YES;
    
    //่Žทๅ–่กจๆƒ…็š„ๅๅญ—
    NSString *faceName = [[_faceArray objectAtIndex:_index] objectForKey:@"cht"];
    
    //ๅฐ†ๅๅญ—ๆ˜พ็คบๅˆฐtextFieldไธŠ
    if ([_delegate respondsToSelector:@selector(faceDidClickedWithName:)]) {
        [_delegate faceDidClickedWithName:faceName];
    }
}
@end



//
//  UIScrollView+Prevent.h
//  QQ
//
//  Created by PXD on 15-5-3.
//  Copyright (c) 2015年 PXD. All rights reserved.
//


#import <UIKit/UIKit.h>


//类别 category
//在已有类的基础之上添加一些新的方法
//继承可以添加方法 和 属性变量
//类别只能添加方法 不能添加属性
//如果类别里面添加的方法和已有类的方法同名  类别里面的方法的优先级最高  覆盖了已有类的方法
@interface UIScrollView (Prevent)


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;


@end


//
//  UIScrollView+Prevent.m
//  QQ
//
//  Created by PXD on 15-5-3.
//  Copyright (c) 2015ๅนด PXD. All rights reserved.
//


#import "UIScrollView+Prevent.h"


@implementation UIScrollView (Prevent)


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
}


@end


//
//  ChatViewController.h
//  QQ
//
//  Created by PXD on 15-4-29.
//  Copyright (c) 2015年 PXD. All rights reserved.
//


#import <UIKit/UIKit.h>
#import "FaceView.h"


@class FriendsModel;//前向声明
@interface ChatViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, FaceViewDelegate>


- (instancetype)initWithNibName:(NSString *)nibNameOrNil
                         bundle:(NSBundle *)nibBundleOrNil
                    friendModel:(FriendsModel *)model;
@end


//
//  ChatViewController.m
//  QQ
//
//  Created by PXD on 15-4-29.
//  Copyright (c) 2015年 PXD. All rights reserved.
//


#import "ChatViewController.h"
#import "FriendsModel.h"
#import "FaceView.h"
#import "AppDelegate.h"


typedef enum {
    kFaceButtonStatusFace = 1,
    kFaceButtonStatusKeyboard
}kFaceButtonStatus;


@interface ChatViewController ()
@property (nonatomic, strong) FriendsModel *model;
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) UITextField *inputTextField;
@property (nonatomic, strong) UITextField *showingTextField;
@property (nonatomic, strong) FaceView *faceView;
@property (nonatomic, strong) UIView *showingOperationView;
@property (nonatomic, strong) UIView *reallyInputOperationView;
@end




@implementation ChatViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil
                         bundle:(NSBundle *)nibBundleOrNil
                    friendModel:(FriendsModel *)model{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.model = model;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiInitial];
    
    //注册(监听)一个键盘弹出来的消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];
    
    //注册(监听)一个键盘隐藏的消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];
}


- (void)keyboardShow:(NSNotification *)notifi{
    //让inputTextField 作为第一响应者
    [_inputTextField becomeFirstResponder];
    
    //tableView上移
    self.myTableView.frame = CGRectMake(0, 20 + 44, 320, 568 - 253 - 44 - 64);
    
    //显示最后一行
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:29 inSection:0];
    [self.myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}


- (void)keyboardHidden:(NSNotification *)notifi{
    //将输入框的内容 显示到 showingTextField上
    _showingTextField.text = _inputTextField.text;
    
    //tableView还原
    self.myTableView.frame = CGRectMake(0, 64, 320, 568-64-44);
}


- (void)uiInitial{
    UIImage *bgImage = [UIImage imageNamed:@"bg"];
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgImageView.image = bgImage;
    [self.view addSubview:bgImageView];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 568-64-44) style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource =self;
    _myTableView.backgroundColor = [UIColor clearColor];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTableView];
    
    //真正的输入视图
    self.reallyInputOperationView = [self createMessageInputOperationView];
    _reallyInputOperationView.frame = CGRectMake(0, 0, 320, 44);
    
    self.inputTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 200, 24)];
    _inputTextField.placeholder = @"message";
    _inputTextField.borderStyle = UITextBorderStyleLine;
    _inputTextField.delegate = self;
    [_reallyInputOperationView addSubview:_inputTextField];
    
    //显示的输入视图
    self.showingOperationView = [self createMessageInputOperationView];
    [self.view addSubview:_showingOperationView];
    
    self.showingTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 200, 24)];
    _showingTextField.placeholder = @"message";
    _showingTextField.borderStyle = UITextBorderStyleLine;
    _showingTextField.inputAccessoryView = _reallyInputOperationView;
    [_showingOperationView addSubview:_showingTextField];
}


- (void)setModel:(FriendsModel *)model{
    if (_model != model) {
        _model  = model;
    }
    //设置导航栏的标题 为联系人的名字
    self.title = _model.friendName;
}


#pragma mark -- TableDelegate&DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 30;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = [NSString stringWithFormat:@"row_%ld", indexPath.row];
    return cell;
}


- (UIView *)createMessageInputOperationView{
    //背景视图
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 568-44, 320, 44)];
    [bgView setBackgroundColor:[UIColor lightTextColor]];
    
    //笑脸
    UIButton *faceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [faceButton setFrame:CGRectMake(240, 7, 30, 30)];
    [faceButton setBackgroundImage:[UIImage imageNamed:@"face"] forState:UIControlStateNormal];
    [faceButton addTarget:self action:@selector(faceButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    faceButton.tag = kFaceButtonStatusKeyboard;
    [bgView addSubview:faceButton];
    
    //add
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [addButton setFrame:CGRectMake(280, 7, 30, 30)];
    [bgView addSubview:addButton];
    
    return bgView;
}


//重写faceView的get方法
- (FaceView *)faceView{
    if (_faceView == nil) {
        _faceView = [[FaceView alloc] initWithFrame:CGRectMake(0, 568-253, 320, 253)];
        _faceView.hidden = YES;
        _faceView.delegate = self;
        [self.view addSubview:_faceView];
    }
    return _faceView;
}


#pragma mark -- FaceButtonAction
- (void)faceButtonDidClicked:(UIButton *)sender{
    if (sender.tag == kFaceButtonStatusKeyboard) {
        //显示表情视图
        self.faceView.hidden = NO;
        //改变button的状态
        sender.tag = kFaceButtonStatusFace;
        
        self.showingOperationView.frame = CGRectMake(0, 568-253-44, 320, 44);
        
        UIButton *showViewButton = (UIButton *)[self.showingOperationView viewWithTag:kFaceButtonStatusKeyboard];
        showViewButton.tag = kFaceButtonStatusFace;
        
        [self.inputTextField resignFirstResponder];
        [self.showingTextField resignFirstResponder];
        
        
    } else {
        //隐藏表情视图
        self.faceView.hidden = YES;
        
        sender.tag = kFaceButtonStatusKeyboard;
        
        [_showingTextField becomeFirstResponder];
        
        self.showingOperationView.frame = CGRectMake(0, 568-44, 320, 44);
        
        UIButton *reallyViewButton = (UIButton *)[self.reallyInputOperationView viewWithTag:kFaceButtonStatusFace];
        reallyViewButton.tag = kFaceButtonStatusKeyboard;
    }
}


#pragma mark -- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //取消textfield的第一响应者
    [_inputTextField resignFirstResponder];
    [_showingTextField resignFirstResponder];
    
    return YES;
}


#pragma mark -- UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //取消textfield的第一响应者
    [_inputTextField resignFirstResponder];
    [_showingTextField resignFirstResponder];
}


#pragma mark -- FaceViewDelegate
- (void)faceDidClickedWithName:(NSString *)faceName{
    _showingTextField.text = [_showingTextField.text stringByAppendingString:faceName];
    _inputTextField.text = _showingTextField.text;
}
@end















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值