//表情面板界面
//头文件
@protocol FaceViewDelegate <NSObject>
-(void)sendFace:(NSString*)aStr;
@end
@interface FaceViewController : UIViewController<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView* scrollView;
@property(nonatomic,assign) id<FaceViewDelegate> FaceViewDelegate;
@end
#import "FaceViewController.h"
@interface FaceViewController ()
@property(nonatomic,strong)NSMutableArray* array;
@end
//实现文件
@implementation FaceViewController
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
-(void)click:(UIButton*)sender
{
if ([_FaceViewDelegate respondsToSelector:@selector(sendFace:)])
{
[_FaceViewDelegate sendFace:_array[sender.tag]];
}
}
-(void)initData
{
int count = 0;
CGFloat space = (self.view.frame.size.width - 24*7)/8;
for (int i = 0;i < 5;i++)
{
for (int j = 0;j < 3;j++)
{
for (int m = 0;m < 7;m++)
{
NSString* str = [NSString stringWithFormat:@"%d.png",count];//表情图片的编号
UIButton* bt = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width*i+m*24 + space*(m+1),j*24+48*(j + 1), 24, 24)];
[bt setBackgroundImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
[bt addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
bt.tag = count;
[_array addObject:str];
[_scrollView addSubview:bt];
count +=1;
}
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
_array = [NSMutableArray new];
_scrollView = [[UIScrollView alloc] init];
_scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, 216);
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(self.view.frame.size.width*5, 216);
[self.view addSubview:_scrollView];
[self initData];
}
@end
#import "ForumViewController.h"
#import "FaceViewController.h"
@interface ForumViewController ()<UITextViewDelegate,FaceViewDelegate>
@property(nonatomic,strong)UITextView* textView;
@property(nonatomic,strong)UIToolbar* toolBar;
@property(nonatomic,strong)FaceViewController* face;
@end
//主界面
//头文件
@interface ForumViewController : UIViewController
@end
//实现文件@implementation ForumViewController
- (id)init
{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.face = [[FaceViewController alloc] init];
self.face.view.frame = CGRectMake(0, self.view.frame.size.height+44, self.view.frame.size.width, 216);
self.face.FaceViewDelegate = self;
[self.view addSubview:self.face.view];
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 300)];
_textView.delegate = self;
_textView.backgroundColor = [UIColor redColor];
[self.view addSubview:_textView];
_toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height+44, self.view.frame.size.width, 44)];
[self.view addSubview:_toolBar];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
UIBarButtonItem *Item;
UIBarButtonItem *Item1;
Item = [[UIBarButtonItem alloc]
initWithTitle:@"diyi"
style:UIBarButtonItemStylePlain
target:self
action:@selector(decrement)];
Item1 = [[UIBarButtonItem alloc]
initWithTitle:@"dier"
style:UIBarButtonItemStylePlain
target:self
action:@selector(increment)];
_toolBar.barStyle = UIBarStyleBlackOpaque;
[_toolBar setItems:@[Item,Item1] animated:YES];
[_toolBar sizeToFit];
// Do any additional setup after loading the view.
}
-(void)increment
{
[_textView becomeFirstResponder];
NSLog(@"increment");
[self autoMovekeyFace:0];
}
-(void)decrement
{
[_textView resignFirstResponder];
[self autoMovekeyBoard:216];
NSLog(@"decrement");
[self autoMovekeyFace:216];
}
-(void) autoMovekeyFace: (float) h
{
if (h)
{
self.face.view.frame = CGRectMake(0, self.view.frame.size.height-h, self.view.frame.size.width, h);
}
else
{
self.face.view.frame = CGRectMake(0, self.view.frame.size.height+44, self.view.frame.size.width, 44);
}
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([textView.text isEqualToString:@"\n"])
{
[_textView resignFirstResponder];
return NO;
}
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self autoMovekeyBoard:keyboardRect.size.height];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[self autoMovekeyBoard:0];
}
-(void) autoMovekeyBoard: (float) h
{
if (h)
{
_toolBar.frame = CGRectMake(0.0f, (float)(self.view.frame.size.height-h-44), 320.0f, 44.0f);
}
else
{
_toolBar.frame = CGRectMake(0.0f, (float)(self.view.frame.size.height+44), 320.0f, 44.0f);
}
}
//实现表情发到文本框中
-(void)sendFace:(NSString*)aStr
{
_textView.text = [NSString stringWithFormat:@"%@[%@]",_textView.text,aStr];
}