自定義表情鍵盤

自定義鍵盤:


-(void)viewDidLoad {

//增加監聽,當鍵盤出現收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification

                                               object:nil];

    

    //增加監聽,當鍵退出時收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification

                                               object:nil];

    //增加監聽,當鍵盤改變時收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];


 int emojiRangeArr[10] = {0xE001, 0xE05A, 0xE101, 0xE15A, 0xE201, 0xE253, 0xE401, 0xE44C, 0xE501, 0xE537};   //表情對應的字符串範圍 

    for (int j = 0; j < 10; j+=2) {

        int startIndex = emojiRangeArr[j];

        int endIndex = emojiRangeArr[j + 1];

        for (int i = startIndex; i <= endIndex; i++) {

            //將表情字符串放入數組中

            [faceArr addObject:[NSString stringWithFormat:@"%C", (unichar)i]];

        }

    }

[self layoutBgView];

}


-(void)layoutBgView {

    bgView = [[UIView allocinitWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height + 30, [UIScreen mainScreen].bounds.size.width30)];

    [self.view addSubview:bgView];

    

    //表情

    UIButton *emojiBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    emojiBtn.frame = CGRectMake(3052020);

    [emojiBtn setImage:[UIImage imageNamed:@"emoji"forState:UIControlStateNormal];

    [emojiBtn addTarget:self action:@selector(showEmoji:) forControlEvents:UIControlEventTouchUpInside];

    [bgView addSubview:emojiBtn];

}


#pragma mark -- 表情按鈕事件(出現表情鍵盤)

-(void)showEmoji:(UIButton *)button {

    //佈局自定義的表情視圖

    [self layoutEmojiView];

    //將鍵盤的inputView設置成自己定義的表情視圖

    contentTF.inputView = emojiView;

    //刷新鍵盤的inputView

    [contentTF reloadInputViews];

/*

如果想讓鍵盤再變成系統鍵盤,只需要把inputView設置成nil,再刷新一下即可,代碼如下:

contentTF.inputView = nil;

       [contentTF reloadInputViews];

*/

}




#pragma mark -- 自定義表情鍵盤

-(void) layoutEmojiView

 {

//emojiView上放了一個pageControl和一個collectionView,pagecontrol用來顯示分頁,collectionView用來顯示一個個的表情

   emojiView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];

    //分頁控制器

    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 170, [UIScreen mainScreen].bounds.size.width, 20)];

    pageControl.numberOfPages = (emojiArr.count / 28) + (emojiArr.count % 28 == 0?0:1);

    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

    pageControl.pageIndicatorTintColor = [UIColor grayColor];

    [emojiView addSubview:pageControl];

    //佈局collectionView,用來顯示錶情

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    flowLayout.itemSize = CGSizeMake(30, 30);

    float xOffset = ([UIScreen mainScreen].bounds.size.width - 7 * 30 - 10 * 6) / 2;

    flowLayout.sectionInset = UIEdgeInsetsMake(10, xOffset, 10, xOffset);

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 160) collectionViewLayout:flowLayout];

    collectionView.pagingEnabled = YES;

    collectionView.tag = 100;

    flowLayout.minimumLineSpacing = 10;

    flowLayout.minimumInteritemSpacing = 5;

    collectionView.showsHorizontalScrollIndicator = NO;

    collectionView.dataSource = self;

    collectionView.delegate = self;

    //註冊cell

    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"emoji"];

    collectionView.backgroundColor = bgView.backgroundColor;

    [emojiView addSubview:collectionView];


}


#pragma mark -- 表情collectionViewitem個數,每頁28個表情

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    if ((faceArr.count / 28) + (faceArr.count % 28 == 0?0:1) != section + 1)  {

        return 28;

    }else {

        return faceArr.count - 28 *((faceArr.count / 28)+(faceArr.count % 28 == 0?0:1) - 1);

    }

}


#pragma mark -- 返回頁數

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return (faceArr.count / 28) + (faceArr.count % 28 == 0?0:1);

}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"emoji" forIndexPath:indexPath];

    for (int i = (int)cell.contentView.subviews.count; i>0; i--) {

        [cell.contentView.subviews[i - 1removeFromSuperview];

    }

    UILabel *label = [[UILabel allocinitWithFrame:CGRectMake(003030)];

    label.font = [UIFont systemFontOfSize:25];

    label.text = faceArr[indexPath.row + indexPath.section * 28];

    [cell.contentView addSubview:label];

    return cell;

}


#pragma mark -- cell的點擊事件,點擊表情

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *str = faceArr[indexPath.section * 28 + indexPath.row];

    contentTF.text = [NSString stringWithFormat:@"%@%@"contentTF.text, str];//將選中的表情在輸入框中顯示出來


}


#pragma mark -- 翻頁後,對分頁控制進行更改

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat contenOffset = scrollView.contentOffset.x;

    int apage = contenOffset/scrollView.frame.size.width+((int)contenOffset%(int)scrollView.frame.size.width==0?0:1);

    pageControl.currentPage = apage;

}


#pragma mark -- 當鍵盤出現時調用

- (void)keyboardWillShow:(NSNotification *)aNotification

{

    //獲取鍵盤的高度

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    keyHeight = keyboardRect.size.height;

    NSLog(@"鍵盤的高度:%ld", (long)keyHeight);

    //改變輸入框的位置

    [UIView animateWithDuration:0.1 animations:^{

        backLabel.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 44 - 40, [UIScreen mainScreen].bounds.size.width50);

        setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 40, [UIScreen mainScreen].bounds.size.width40);

    }];

    

}


#pragma mark -- 當鍵盤退出時調用

- (void)keyboardWillHide:(NSNotification *)aNotification

{

    //改變輸入框的位置

    [UIView animateWithDuration:0.1 animations:^{

        backLabel.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width50);

    }];

    //獲取鍵盤的高度

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    keyHeight = keyboardRect.size.height;

    NSLog(@"鍵盤的高度:%ld", (long)keyHeight);

    //改變輸入框的位置

    [UIView animateWithDuration:0.1 animations:^{

        setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height + 40, [UIScreen mainScreen].bounds.size.width40);

    }];

}


#pragma mark -- 當鍵盤改變時調用

-(void)keyboardChange:(NSNotification *)noti {

    //獲取鍵盤的高度

    NSDictionary *userInfo = [noti userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    keyHeight = keyboardRect.size.height;

    NSLog(@"鍵盤的高度:%ld", (long)keyHeight);

    //改變輸入框的位置

    [UIView animateWithDuration:0.1 animations:^{

        backLabel.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 44 - 40, [UIScreen mainScreen].bounds.size.width50);

        setView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - keyHeight - 40, [UIScreen mainScreen].bounds.size.width40);

    }];

}

效果圖:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章