TSGennertatorSelectStyleVC.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // TSGennertatorSelectStyleVC.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/4/27.
  6. //
  7. class TSGennertatorSelectStyleVC: TSBaseVC {
  8. enum Style {
  9. case ptp
  10. case ttp
  11. }
  12. lazy var bottomView: UIView = {
  13. let bottomView = UIView()
  14. bottomView.backgroundColor = .popupColor
  15. return bottomView
  16. }()
  17. lazy var xBtn: TSUIExpandedTouchButton = {
  18. let xBtn = TSUIExpandedTouchButton()
  19. xBtn.setUpButton(image: UIImage(named: "close_clear")) { [weak self] in
  20. guard let self = self else { return }
  21. self.dismiss(animated: true)
  22. }
  23. return xBtn
  24. }()
  25. var style:Style = .ptp
  26. var selectedValueBlock:((IndexPath,TSGenerateStyleModel)->Void)?
  27. var dataArray: [TSGenerateStyleModel] = [TSGenerateStyleModel](){
  28. didSet{
  29. styleCollectionView.reloadData()
  30. if dataArray.count > 0 {
  31. self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: false, scrollPosition: .centeredVertically)
  32. }
  33. }
  34. }
  35. lazy var layout: UICollectionViewFlowLayout = {
  36. let w = (k_ScreenWidth-32.0-30.0-2.0)/4.0
  37. let layout = UICollectionViewFlowLayout()
  38. layout.itemSize = CGSize(width: w, height: 108)
  39. layout.scrollDirection = .vertical
  40. layout.minimumInteritemSpacing = 0.0
  41. layout.minimumLineSpacing = 4.0
  42. layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: k_Height_safeAreaInsetsBottom(), right: 16)
  43. return layout
  44. }()
  45. lazy var styleCollectionView: UICollectionView = {
  46. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  47. collectionView.delegate = self
  48. collectionView.dataSource = self
  49. collectionView.showsVerticalScrollIndicator = false
  50. collectionView.showsHorizontalScrollIndicator = false
  51. collectionView.backgroundColor = .clear
  52. collectionView.register(TSGennertatorSelectStyleCell.self, forCellWithReuseIdentifier: TSGennertatorSelectStyleCell.cellID)
  53. if #available(iOS 11.0, *) {
  54. collectionView.contentInsetAdjustmentBehavior = .never
  55. }
  56. return collectionView
  57. }()
  58. var currentIndexPath:IndexPath = IndexPath(item: 0, section: 0)
  59. override func createView() {
  60. self.view.backgroundColor = .clear
  61. self.contentView.backgroundColor = .clear
  62. setNavBarViewHidden(true)
  63. contentView.addSubview(bottomView)
  64. bottomView.snp.makeConstraints { make in
  65. make.bottom.leading.trailing.bottom.equalTo(0)
  66. make.top.equalTo(104+k_Nav_Height)
  67. }
  68. setContentView()
  69. kDelayMainShort {
  70. self.bottomView.cornersRound(radius: 20, corner: [.topLeft,.topRight])
  71. }
  72. let topView = UIView()
  73. topView.backgroundColor = .clear
  74. contentView.addSubview(topView)
  75. topView.snp.makeConstraints { make in
  76. make.top.leading.trailing.equalTo(0)
  77. make.bottom.equalTo(bottomView.snp.top)
  78. }
  79. topView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickView)))
  80. }
  81. @objc func clickView(){
  82. self.dismiss(animated: true)
  83. }
  84. func setContentView(){
  85. let titleLabel = UILabel.createLabel(text: "Select Style".localized,font: .font(size: 16,weight: .medium),textColor: .white)
  86. bottomView.addSubview(titleLabel)
  87. titleLabel.snp.makeConstraints { make in
  88. make.leading.equalTo(16)
  89. make.top.equalTo(24)
  90. }
  91. bottomView.addSubview(xBtn)
  92. xBtn.snp.makeConstraints { make in
  93. make.top.equalTo(8)
  94. make.trailing.equalTo(-8)
  95. make.width.height.equalTo(24)
  96. }
  97. bottomView.addSubview(styleCollectionView)
  98. styleCollectionView.snp.makeConstraints { make in
  99. make.top.equalTo(56)
  100. make.leading.trailing.equalTo(0)
  101. make.bottom.equalTo(0)
  102. }
  103. }
  104. override func dealThings() {
  105. }
  106. }
  107. extension TSGennertatorSelectStyleVC: UICollectionViewDataSource ,UICollectionViewDelegate {
  108. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  109. return 1
  110. }
  111. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  112. return dataArray.count
  113. }
  114. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  115. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSGennertatorSelectStyleCell.cellID, for: indexPath)
  116. if let cell = cell as? TSGennertatorSelectStyleCell,let itemModel = dataArray.safeObj(At: indexPath.item){
  117. cell.style = style
  118. cell.itemModel = itemModel
  119. }
  120. return cell
  121. }
  122. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  123. if let model = dataArray.safeObj(At: indexPath.item){
  124. currentIndexPath = indexPath
  125. self.styleCollectionView.selectItem(at: self.currentIndexPath, animated: true, scrollPosition: .centeredVertically)
  126. selectedValueBlock?(indexPath,model)
  127. }
  128. }
  129. }
  130. class TSGennertatorSelectStyleCell: TSBaseCollectionCell {
  131. static let cellID = "TSGennertatorSelectStyleCell"
  132. override var isSelected: Bool{
  133. didSet{
  134. boardImageView.isHidden = isSelected ? false : true
  135. textLabel.textColor = isSelected ? .themeColor : .white.withAlphaComponent(0.8)
  136. }
  137. }
  138. var style:TSGennertatorSelectStyleVC.Style = .ptp{
  139. didSet{
  140. switch style {
  141. case .ptp:
  142. boardImageView.cornerRadius = 16
  143. let w = 78
  144. boardImageView.snp.updateConstraints { make in
  145. make.width.height.equalTo(w)
  146. }
  147. imageView.snp.updateConstraints { make in
  148. make.top.equalTo(4)
  149. make.width.height.equalTo(w-8)
  150. }
  151. case .ttp:
  152. boardImageView.cornerRadius = 34.0
  153. let w = 68.0
  154. boardImageView.snp.updateConstraints { make in
  155. make.width.height.equalTo(w)
  156. }
  157. imageView.snp.updateConstraints { make in
  158. make.top.equalTo(4)
  159. make.width.height.equalTo(w-8)
  160. }
  161. }
  162. }
  163. }
  164. var itemModel:TSGenerateStyleModel = TSGenerateStyleModel(){
  165. didSet{
  166. imageView.image = UIImage(named: itemModel.imageName)
  167. let hotImageNamed = getSpecialStyleImageNamed(specialStyle: itemModel.specialStyle)
  168. if hotImageNamed.count > 0 {
  169. hotImageView.image = UIImage(named: hotImageNamed)
  170. hotImageView.isHidden = false
  171. }else{
  172. hotImageView.isHidden = true
  173. }
  174. textLabel.text = itemModel.imageText.localized
  175. }
  176. }
  177. func getSpecialStyleImageNamed(specialStyle:Int)->String{
  178. switch style {
  179. case .ptp:
  180. if specialStyle == 1 {
  181. return "ptp_style_hot"
  182. }else if specialStyle == 2 {
  183. return "ptp_style_new"
  184. }else if specialStyle == 3 {
  185. return "ptp_style_max"
  186. }
  187. case .ttp:
  188. if specialStyle == 1 {
  189. return "ttp_style_hot"
  190. }else if specialStyle == 2 {
  191. return "ptp_style_new"
  192. }else if specialStyle == 3 {
  193. return "ptp_style_max"
  194. }
  195. }
  196. return ""
  197. }
  198. lazy var boardImageView: UIImageView = {
  199. let boardImageView = UIImageView()
  200. boardImageView.cornerRadius = 16
  201. boardImageView.isHidden = true
  202. return boardImageView
  203. }()
  204. lazy var imageView: UIImageView = {
  205. let imageView = UIImageView()
  206. imageView.cornerRadius = 12
  207. return imageView
  208. }()
  209. lazy var hotImageView: UIImageView = {
  210. let hotImageView = UIImageView.createImageView(imageName: "ptp_style_hot")
  211. hotImageView.isHidden = true
  212. return hotImageView
  213. }()
  214. lazy var textLabel: UILabel = {
  215. let textLabel = UILabel.createLabel(font: .font(size: 12),textColor: .white.withAlphaComponent(0.8),textAlignment: .center,numberOfLines: 2)
  216. return textLabel
  217. }()
  218. override func creatUI() {
  219. // let w = contentView.width
  220. let w = 78
  221. bgContentView.addSubview(boardImageView)
  222. boardImageView.snp.makeConstraints { make in
  223. make.top.centerX.equalToSuperview()
  224. make.width.height.equalTo(w)
  225. }
  226. bgContentView.addSubview(imageView)
  227. imageView.snp.makeConstraints { make in
  228. make.top.equalTo(4)
  229. make.centerX.equalToSuperview()
  230. make.width.height.equalTo(w-8)
  231. }
  232. imageView.addSubview(hotImageView)
  233. hotImageView.snp.makeConstraints { make in
  234. make.leading.equalTo(0)
  235. make.top.equalTo(0.0)
  236. make.width.height.equalTo(36)
  237. }
  238. bgContentView.addSubview(textLabel)
  239. textLabel.snp.makeConstraints { make in
  240. make.top.equalTo(imageView.snp.bottom).offset(2)
  241. make.leading.trailing.bottom.equalToSuperview()
  242. }
  243. boardImageView.setNeedsLayout()
  244. DispatchQueue.main.async {
  245. self.boardImageView.addGradientBorder(colors: ["#FA794F".uiColor,"#F8C32A".uiColor,"#FEFBF4".uiColor],width: 1.5,startPoint: CGPoint(x: 0, y: 1),endPoint: CGPoint(x: 1, y: 0))
  246. }
  247. }
  248. }