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

显示图片表情

  • CZEmoticonButtonemoticon 属性的 didSet 里面判断如果不是 emoji 就设置图片

    class HMEmoticonButton: UIButton {
      /// 表情模型
      var emoticon: HMEmoticonModel? {
          didSet {
              if emoticon!.emoji != nil {  // emoji 表情
                  self.setTitle(emoticon!.emoji!, for: .normal)
              } else {    // 图片表情
                  // 清空文字
                  self.setTitle(nil, for: .normal)
    
                  // 设置图片
                  self.setImage(UIImage(named: emoticon!.png!), for: .normal)
              }
          }
      }
    }
    
  • 运行,图片并没有设置成功,因为图片的路径不对.在模型里面添加 pngPath 保存图片的完整路径
    /// png的完整路径 = bundlePath/表情包文件夹/png
    var fullPngPath: String?
    
  • 图片的完整路径等于EmoticonBundlePath/表情包文件夹/png, CZEmoticonModel 需要添加一个属性来记录当前模型的文件夹路径
    /// 表情包 文件夹 名称
    var id: String
    
  • 字典转模型构造函数 添加一个参数 id

    // MARK: - 字典转模型
    init(id: String, dict: [String: String]) {
      self.id = id
      super.init()
    
      // KVC
      setValuesForKeys(dict)
    }
    
  • HMEmoticonManagerloadPackage: 方法创建表情的时候传入 id
    // 遍历,加载表情模型
    for dict in emoticonsArr {
      // 表情字典 转 模型, 添加到表情模型数组里面
      emoticons.append(HMEmoticonModel(id: id, dict: dict))
    }
    
  • CZEmoticonModelpng 属性的 didSet 方法中来设置 fullPngPath
    /// 表情图片名称, 在手机上显示表情图片
    var png: String? {
      didSet {
          // 图片完整路径 = EmoticonBundlePath/表情包文件夹/png
          fullPngPath = bundlePath + "/" + id + "/" + png!
      }
    }
    
  • HMEmoticonButtonemoticon 属性的 didSet 里面使用 fullPngPath 来设置图片

    class HMEmoticonButton: UIButton {
      /// 表情模型
      var emoticon: HMEmoticonModel? {
          didSet {
              if emoticon!.emoji != nil {  // emoji 表情
                  self.setTitle(emoticon!.emoji!, for: .normal)
              } else {    // 图片表情
                  // 清空文字
                  self.setTitle(nil, for: .normal)
    
                  // 设置图片
                  self.setImage(UIImage(named: emoticon!.fullPngPath!), for: .normal)
              }
          }
      }
    }
    
  • 运行图片可以显示, 但是emoji 表情的按钮也会显示图片,需要在设置 emoji 表情的时候也需要清空图片

    class HMEmoticonButton: UIButton {
      /// 表情模型
      var emoticon: HMEmoticonModel? {
          didSet {
              if emoticon!.emoji != nil {  // emoji 表情
                  self.setTitle(emoticon!.emoji!, for: .normal)
    
                  // 清空图片
                  self.setImage(nil, for: .normal)
              } else {    // 图片表情
                  // 清空文字
                  self.setTitle(nil, for: .normal)
    
                  // 设置图片
                  self.setImage(UIImage(named: emoticon!.fullPngPath!), for: .normal)
              }
          }
      }
    }
    
  • 发现 默认 表情包最后一页只有2个表情.但是却显示了20个表情,在设置表情的时候需要先隐藏所有按钮,当有内容的按钮才需要显示出来

    /// 表情模型数组
    var emoticons: [CZEmoticonModel]? {
      didSet {
          // 先隐藏所有的按钮
          for button in emoticonButtons {
              button.hidden = true
          }
    
          // 设置按钮的图片,要显示图片或emoji的按钮,就不隐藏
          for (index, emoticon) in emotionMoldes.enumerate() {
              // 获取每个模型
              // 找到对应的按钮
              let emoticonButton = emoticonButtons[index]
              emoticonButton.emoticon = emoticon
              emoticonButton.hidden = false
          }
      }
    }
    

results matching ""

    No results matching ""