• 新浪微博客户端(44)-分页表情键盘


    DJEmotionListView.m

    #import "DJEmotionListView.h"
    
    // 单页表情显示数量
    #define DJEmotionPageSize 20
    
    
    
    @interface DJEmotionListView() <UIScrollViewDelegate>
    
    /** Emotion 上部滑动区域 */
    @property (nonatomic,weak) UIScrollView *emotionScrollView;
    /** Emotion 底部pageControl */
    @property (nonatomic,weak) UIPageControl *emotionPageControl;
    
    @end
    
    
    @implementation DJEmotionListView
    
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            
           UIScrollView *emotionScrollView = [[UIScrollView alloc] init];
            [self addSubview:emotionScrollView];
            self.emotionScrollView = emotionScrollView;
            emotionScrollView.backgroundColor = [UIColor redColor];
            // 设置scrollView水平和垂直方向上的滚动条为空
            emotionScrollView.showsHorizontalScrollIndicator = NO;
            emotionScrollView.showsVerticalScrollIndicator = NO;
            // 设置scrollView 分页
            emotionScrollView.pagingEnabled = YES;
            emotionScrollView.delegate = self;
            
            
            UIPageControl *emotionPageControl = [[UIPageControl alloc] init];
            [self addSubview:emotionPageControl];
            self.emotionPageControl = emotionPageControl;
             emotionPageControl.backgroundColor = [UIColor blueColor];
            emotionPageControl.userInteractionEnabled = NO; // 设置pageContorl不可点击
    
            // 使用KVC自定义pageControl 显示图片
            [emotionPageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_normal"] forKeyPath:@"_pageImage"];
            [emotionPageControl setValue:[UIImage imageNamed:@"compose_keyboard_dot_selected"] forKeyPath:@"_currentPageImage"];
            
            
        }
        return self;
    }
    
    
    
    
    
    - (void)layoutSubviews {
    
        [super layoutSubviews];
        
        // emotionPageControl
        CGFloat pageControlW = self.width;
        CGFloat pageControlH = 35;
        CGFloat pageControlX = 0;
        CGFloat pageControlY = self.height - pageControlH;
        self.emotionPageControl.frame = CGRectMake(pageControlX, pageControlY, pageControlW, pageControlH);
        
        // emotionScrollView
        CGFloat scrollViewW = self.width;
        CGFloat scrollViewH = pageControlY;
        CGFloat scrollViewX = 0;
        CGFloat scrollViewY = 0;
        self.emotionScrollView.frame = CGRectMake(scrollViewX, scrollViewY, scrollViewW, scrollViewH);
        
        // pageView
        NSUInteger count = self.emotionScrollView.subviews.count;
        CGFloat pageViewW = self.emotionScrollView.width;
        CGFloat pageViewH = self.emotionScrollView.height;
        CGFloat pageViewY = self.emotionScrollView.y;
        
        for (int i = 0; i < count; i++) {
            UIView *pageView = self.emotionScrollView.subviews[i];
            pageView.x = i * pageViewW;
            pageView.y = pageViewY;
            pageView.width = pageViewW;
            pageView.height = pageViewH;
        }
    
        self.emotionScrollView.contentSize = CGSizeMake(scrollViewW * count, 0);
    
    }
    
    
    
    - (void)setEmotions:(NSArray *)emotions {
    
        
        _emotions = emotions;
        // emotion 总个数
        NSUInteger emotionCount = emotions.count;
        // 页码总个数
        NSUInteger pageNums = (emotionCount + DJEmotionPageSize - 1) / DJEmotionPageSize;
        
        // 更新pageControl 显示个数
        self.emotionPageControl.numberOfPages = pageNums;
        
        // 为scrollView 添加子View
        for (int i = 0; i < pageNums; i++) {
            UIView *pageView = [[UIView alloc] init];
            pageView.backgroundColor = DJRandomColor;
            [self.emotionScrollView addSubview:pageView];
        }
        
    }
    
    #pragma mark - UIScrollView 代理方法
    
    /** 通过监听scrollView滚动来更新pageControl状态 */
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
        double page = scrollView.contentOffset.x / scrollView.width;
        self.emotionPageControl.currentPage = (int)(page + 0.5);
        
    }
    
    
    
    
    @end

    最终效果:

     

      

  • 相关阅读:
    hdu3001 Travelling
    android 对一个合并后的联系人选择编辑,手机屏幕会缓慢变暗后再进入编辑界面的问题
    数组指针与指针数组 函数指针与指针函数
    CF:322D
    QRadioButton类中Toggled()信号的使用方法
    Android---App Widget(四)
    github 坑爹的仓库初始化设置
    Failed to load session “ubuntu” -- 12.04
    Linux下的图形界面——X Window的安装
    用户管理操作示例
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/6115386.html
Copyright © 2020-2023  润新知