新浪微博-表情键盘的显示
2015-11-02 16:40
323 查看
</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]; } }
相关文章推荐
- Android 中文API —— Spinner
- Java并发包中的读写锁及其实现分析
- Invalidate() postInvalidate() 刷新界面方法
- 图片上传
- 获取android系统服务
- 程序人生【一些经典的资料】
- IOS 沙盒与清除缓存
- Heap:左式堆的应用例(任意序列变单调性最小价值)
- 两个按钮共用一个界面并且点击该界面保存按钮调用不同事件
- oracle连接问题【转载】
- 多线程——生产者和消费者
- php数据库编程---mysqli扩展库
- VC获取邮件存为eml文件到本地
- 第十周项目1 二叉树的链式存储及基本运算
- MyBatis缓存分为一级缓存和二级缓存
- <context annotation-config />标签意义
- 关于瀑布流的若干想法
- tinyxml的一个bug
- MYSQL FIND_IN_SET()
- windows监控——再见zmq