专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

表情包模型分页处理

  • 每个表情包需要显示多组.所以需要将表情包数据分成多组
  • CZEmoticonPackageModel 提供一个属性 pageEmoticons 给别人访问.这个属性就是分页后的表情数组, 一个表情包有多页,每页有20个表情

    /// 表情包有所有页数据,每页20个表情
    var pageEmoticons: [[HMEmoticonModel]] = [[HMEmoticonModel]]()
    init(id: String, groupName: String, emoticons: [HMEmoticonModel]) {
      self.id = id
      self.group_name_cn = groupName
      self.emoticons = emoticons
      super.init()
    
      spliteEmoticns()
    }
    
    /// 将表情模型数组拆封成多页
    private func spliteEmoticns() {
      // 计算总页数: 总个数 + 一页个数 - 1 / 一页个数
      let pageCount = (emoticons.count + HMEmoticonNumberOfPage - 1) / HMEmoticonNumberOfPage
    
      // 判断,如果一页表情都没有
      if pageCount == 0 {
          // 创建一页空的表情.
          let emptyEmoticon = [HMEmoticonModel]()
          pageEmoticons.append(emptyEmoticon)
          return
      }
    
      // 遍历,拆分每一页
      for i in 0..<pageCount {
          let location = i * HMEmoticonNumberOfPage
    
          // 判断最后一页截取的长度是否越界
          var length = HMEmoticonNumberOfPage
    
          // 当最后也得起始值+每页的数量 > 表情总数,说明会越界
          if location + HMEmoticonNumberOfPage > emoticons.count {
              length = emoticons.count - location
          }
    
          let range = NSRange(location: location, length: length)
    
          // 一页表情
          let spliteEmoticons = (emoticons as NSArray).subarray(with: range) as! [HMEmoticonModel]
          pageEmoticons.append(spliteEmoticons)
      }
    }
    

results matching ""

    No results matching ""