新浪微博-表情键盘的显示

</pre>自定义表情键盘keyboardView,上部分为一个整体的自定义listView包括UIScrollVIew和pageControll,下部分为一个自定义的toolbar,UIScrollVIew里添加了多个gridView(即一页表情的view),每一个gridView中包含多个emotionView(自定义继承于UIButton),每一个emotionView就是一个表情,emotionView为什么继承与UIButton,因为表情中有emoji表情和图片表情,emoji就是字符串,使用unicode编码而已,UIButton既能够显示图片和文字所以最合适,重点是循环利用,键盘keyboardView有多个表情数组,每点击最底部toolbar时都赋值给listVIew一个表情数组,重写set方法,利用数组个数判断需要多少页,就创建多少个gridView,重点就在这,每次都需要判断UIScrollView的个数(注意:如果直接利用UIScrollView的subviews会包括两个滚动条需要不显示两个滚动条),如果够就不创建,并将多余的显示,如果不够才创建,然后把数组中的表情分发给GridView,也是重写set方法,计算表情个数,这时候需要一个数组,保存所有的emotionview,为的是和gridview中的删除按钮单独出来,方便计算frame,然后则同上不够则创建,emotionview则需要判断是emoji还是图片显示即可。<p></p><pre name="code" class="objc">LIstView计算有多少个GridView
-(void)setEmotions:(NSArray *)emotions
{
    _emotions=emotions;
    
    self.scrollView.contentOffset=CGPointZero;
//    总共有多少个表情
    int count=emotions.count;
    int totlaPage=(count+LSEmotionPerMaxCount-1)/LSEmotionPerMaxCount;
//      LSLog(@"totaalPage===%d",totlaPage);
//    设置pageControl的总页数
    self.pageControl.numberOfPages=totlaPage;
    
    int currentPage=self.scrollView.subviews.count;
    self.scrollView.contentSize= CGSizeMake(self.scrollView.width*totlaPage,0);
//    创建一页表情的GridView
    for (int i=0; i<totlaPage; i++) {
        LSEmotionGridView *gridView=nil;
        
        
        if (i>=currentPage) {
            gridView=[[LSEmotionGridView alloc]init];
            [self.scrollView addSubview:gridView];
        }else{
            gridView=self.scrollView.subviews[i];
        }

        int location=i*LSEmotionPerMaxCount;
        int length=LSEmotionPerMaxCount;
        if (location+length>count) {
            length=count-location;
        }
        NSRange range=NSMakeRange(location, length);
        NSArray *gridEmotions=[emotions subarrayWithRange:range];
        gridView.emotions=gridEmotions;
        gridView.hidden=NO;
    }
    for (int i=totlaPage; i<self.scrollView.subviews.count; i++) {
        LSEmotionGridView *gridView = self.scrollView.subviews[i];
        gridView.hidden = YES;
    }
    [self setNeedsLayout];
    
}
<pre name="code" class="objc">LIstView计算子控件frame
-(void)layoutSubviews{ [super layoutSubviews]; self.pageControl.width=self.width; self.pageControl.height=30; self.pageControl.y=self.height-self.pageControl.height; self.scrollView.width=self.width; self.scrollView.height=self.height-self.pageControl.height; int count=self.scrollView.subviews.count; for (int i=0; i<count; i++) { UIView *gridView=self.scrollView. subviews[i]; gridView.width=self.scrollView.width; gridView.height=self.scrollView.height; gridView.x=i*gridView.width; }}
 
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">GridView计算有多少个表情
<pre name="code" class="objc">-(void)setEmotions:(NSArray *)emotions
{
    _emotions=emotions;
    
    int count=(int)emotions.count;
    int currentCount=(int)self.emotionViews.count;
    
    for (int i=0; i<count; i++) {
        LSEmotionView *emotionView=nil;
        if (i>=currentCount) {
            
            emotionView=[[LSEmotionView alloc]init];
            
            [emotionView addTarget:self action:@selector(emotionViewClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.emotionViews addObject:emotionView];
            [self addSubview:emotionView];
        }else{
            emotionView=self.emotionViews[i];
        }
        emotionView.emotion=emotions[i];
        emotionView.hidden=NO;
    }
    for (int i=count; i<self.emotionViews.count; i++) {
        LSEmotionView *emotionView= self.emotionViews[i];
        emotionView.hidden=YES;
    }
    
}


 
</pre><pre name="code" class="objc"><pre name="code" class="objc">GridView子控件的frame计算
 
-(void)layoutSubviews
{
    [super layoutSubviews];
    //    设置每一个小表情的frame
    CGFloat leftMargin=15;
    CGFloat topMargin=15;
    CGFloat width=(self.width-2*leftMargin)/LSEmotionPerColMaxCount;
    CGFloat height=(self.height-2*topMargin)/LSEmotionPerRolMaxCount;
    for (int i=0; i<self.emotionViews.count; i++) {
        LSEmotionView *emotionView=self.emotionViews[i];
        emotionView.width=width;
        emotionView.height=height;
        emotionView.x=leftMargin+i%LSEmotionPerColMaxCount*width;
        emotionView.y=topMargin+i/LSEmotionPerColMaxCount*height;
        
    }
    //    设置删除按钮的frame
    self.deletedButton.height=height;
    self.deletedButton.width=width;
    self.deletedButton.x=self.width-leftMargin-self.deletedButton.width;
    self.deletedButton.y=self.height-topMargin-self.deletedButton.height;
    
}

emotiionView注意,不管是显示图片还是emoji都需要把相反的清空,因为有可能上次显示图片,这次显示emoji就重复l

-(void)setEmotion:(LSEmotion *)emotion
{
    _emotion=emotion;
    if (emotion.code) {
        
        [UIView setAnimationsEnabled:NO];
        [self setTitle:[NSString emojiWithStringCode:emotion.code]forState:UIControlStateNormal];
        [self setImage:nil forState:UIControlStateNormal];
        [UIView setAnimationsEnabled:YES];
        
    }else {
          [UIView setAnimationsEnabled:NO];
        [self setTitle:nil forState:UIControlStateNormal];
       [self setImage:[UIImage imageWithOriginalName:emotion.png] forState:UIControlStateNormal];
         [UIView setAnimationsEnabled:YES];
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值