#import "ViewController.h"
#define LEFT_FACEKEYBOARD_GAP 20
#define UPDOWN_FACEKEYBOARD_GAP 20
#define LEFTRIGHT_FACEKEYBOARD_GAP 15
#define UP_FACEKEYBOARD_GAP 10
#define FACE_HEIGHT 27
#define FACE_WIDTH 27
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化对应的表情对应输入码数组 22
self.faceArray = [NSArray arrayWithObjects:@"",
@"\ue056",
@"\ue414",
@"\ue402",
@"\ue403",
@"\ue404",
@"\ue405",
@"\ue401",
@"\ue40b",
@"\ue40c",
@"\ue40d",
@"\ue40e",
@"\ue40f",
@"\ue410",
@"\ue411",
@"\ue409",
@"\ue408",
@"\ue407",
@"\ue406",
@"\ue412",
@"\ue413",
@"\ue415",
@"\ue417",nil];
//输入区域TextView初始化
self.inputFaceTextView = [[[UITextView alloc] initWithFrame:CGRectMake(0, 200, 320, 100)] autorelease];
[self.view addSubview:self.inputFaceTextView];
//键盘背景
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 148)];
iv.image = [UIImage imageNamed:@"faceKeyboard_background"];
iv.userInteractionEnabled = YES;
//表情键盘初始化
self.faceKeyboardScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 148)] autorelease];
self.faceKeyboardScrollView.delegate = self;
self.faceKeyboardScrollView.contentSize = CGSizeMake(320 * 4, 148);
self.faceKeyboardScrollView.pagingEnabled = YES;
self.faceKeyboardScrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"faceKeyboard_background"]];
[iv addSubview:self.faceKeyboardScrollView];
[self.view addSubview:iv];
[iv release];
//分页标示初始化
self.pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 128, 320, 20)] autorelease];
self.pageControl.numberOfPages = 4;
self.pageControl.currentPage = 0;
[self.pageControl addTarget:self action:@selector(chagePage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.pageControl];
//键盘上增加表情按钮
CGSize faceKeyboardSize = self.faceKeyboardScrollView.frame.size;
for(int page = 0; page < 2;page++)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 7;j++)
{
UIButton *deleteButton = nil;
UIButton *faceButton = nil;
int faceIndex = page*21 + i*7 + j + 1;
if(faceIndex == 21 * (page + 1))
{
deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.tag = 21;
[deleteButton setImage:[UIImage imageNamed:@"deleteface"] forState:UIControlStateNormal];
deleteButton.frame = CGRectMake(page*faceKeyboardSize.width +LEFT_FACEKEYBOARD_GAP+6*(FACE_WIDTH + LEFTRIGHT_FACEKEYBOARD_GAP) - 3,UP_FACEKEYBOARD_GAP + 2*(FACE_HEIGHT+UPDOWN_FACEKEYBOARD_GAP) + 2,FACE_WIDTH+ 5,FACE_HEIGHT - 3);
[deleteButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.faceKeyboardScrollView addSubview:deleteButton];
}
else
{
int facePageNameInt = page * 20 + i * 7 + j + 1;
NSString *faceName = [NSString stringWithFormat:@"%02d", facePageNameInt];
faceButton = [UIButton buttonWithType:UIButtonTypeCustom];
faceButton.tag = facePageNameInt + 100;
[faceButton addTarget:self action:@selector(faceButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[faceButton setImage:[UIImage imageNamed:faceName] forState:UIControlStateNormal];
faceButton.frame = CGRectMake(page*faceKeyboardSize.width +LEFT_FACEKEYBOARD_GAP+j*(FACE_WIDTH + LEFTRIGHT_FACEKEYBOARD_GAP) - 3,UP_FACEKEYBOARD_GAP + i*(FACE_HEIGHT+UPDOWN_FACEKEYBOARD_GAP) + 2,FACE_WIDTH,FACE_HEIGHT);
[self.faceKeyboardScrollView addSubview:faceButton];
}
}
}
}
}
- (void)faceButtonClicked:(UIButton *)btn
{
int faceIdentifier = btn.tag - 100;
NSString *tempFaceString;
if(faceIdentifier < self.faceArray.count)
{
tempFaceString = [self.faceArray objectAtIndex:faceIdentifier];
self.inputFaceTextView.text = [NSString stringWithFormat:@"%@%@",self.inputFaceTextView.text,tempFaceString];
}
}
- (void)deleteButtonPressed:(UIButton *)deleteBtn
{
NSInteger textViewLength = [self.inputFaceTextView.text length];
NSInteger afterDeleteLength = textViewLength - 1;
if(afterDeleteLength >= 0)
{
NSString *afterDeleteString = [self.inputFaceTextView.text substringToIndex:afterDeleteLength];
self.inputFaceTextView.text = afterDeleteString;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int pageIndex = (scrollView.contentOffset.x + 160) / 320;
self.pageControl.currentPage = pageIndex;
NSLog(@"scrollView = %d",pageIndex);
[self.pageControl updateCurrentPageDisplay];
}
- (void)chagePage:(UIPageControl *)pageControl
{
CGSize scrollViewSize = self.faceKeyboardScrollView.frame.size;
CGRect scrollViewPageRect = CGRectMake(self.pageControl.currentPage * 320, 0, scrollViewSize.width, scrollViewSize.height);
[self.faceKeyboardScrollView scrollRectToVisible:scrollViewPageRect animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end