TSGennertatorSelectStyleVC.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. clickView()
  128. }
  129. }
  130. }
  131. class TSGennertatorSelectStyleCell: TSBaseCollectionCell {
  132. static let cellID = "TSGennertatorSelectStyleCell"
  133. override var isSelected: Bool{
  134. didSet{
  135. boardImageView.isHidden = isSelected ? false : true
  136. checkView.isHidden = boardImageView.isHidden
  137. textLabel.textColor = isSelected ? .themeColor : .white.withAlphaComponent(0.8)
  138. }
  139. }
  140. var style:TSGennertatorSelectStyleVC.Style = .ptp{
  141. didSet{
  142. switch style {
  143. case .ptp:
  144. boardImageView.cornerRadius = 16
  145. let w = 78
  146. boardImageView.snp.updateConstraints { make in
  147. make.width.height.equalTo(w)
  148. }
  149. imageView.snp.updateConstraints { make in
  150. make.top.equalTo(4)
  151. make.width.height.equalTo(w-8)
  152. }
  153. imageView.cornerRadius = 12
  154. hotImageView.snp.updateConstraints { make in
  155. make.width.height.equalTo(36)
  156. }
  157. case .ttp:
  158. boardImageView.cornerRadius = 34.0
  159. let w = 76.0
  160. boardImageView.snp.updateConstraints { make in
  161. make.width.height.equalTo(w)
  162. }
  163. imageView.snp.updateConstraints { make in
  164. make.top.equalTo(4)
  165. make.width.height.equalTo(w-8)
  166. }
  167. imageView.cornerRadius = (w-8)/2
  168. hotImageView.snp.updateConstraints { make in
  169. make.width.height.equalTo(34)
  170. }
  171. }
  172. }
  173. }
  174. var itemModel:TSGenerateStyleModel = TSGenerateStyleModel(){
  175. didSet{
  176. imageView.image = UIImage(named: itemModel.imageName)
  177. let hotImageNamed = getSpecialStyleImageNamed(specialStyle: itemModel.specialStyle)
  178. if hotImageNamed.count > 0 {
  179. hotImageView.image = UIImage(named: hotImageNamed)
  180. hotImageView.isHidden = false
  181. }else{
  182. hotImageView.isHidden = true
  183. }
  184. textLabel.text = itemModel.imageText.localized
  185. }
  186. }
  187. func getSpecialStyleImageNamed(specialStyle:Int)->String{
  188. switch style {
  189. case .ptp:
  190. if specialStyle == 1 {
  191. return "ptp_style_hot"
  192. }else if specialStyle == 2 {
  193. return "ptp_style_new"
  194. }else if specialStyle == 3 {
  195. return "ptp_style_max"
  196. }
  197. case .ttp:
  198. if specialStyle == 1 {
  199. return "ttp_style_hot"
  200. }else if specialStyle == 2 {
  201. return "ptp_style_new"
  202. }else if specialStyle == 3 {
  203. return "ptp_style_max"
  204. }
  205. }
  206. return ""
  207. }
  208. lazy var boardImageView: UIImageView = {
  209. let boardImageView = UIImageView()
  210. boardImageView.cornerRadius = 16
  211. boardImageView.isHidden = true
  212. return boardImageView
  213. }()
  214. lazy var imageView: UIImageView = {
  215. let imageView = UIImageView()
  216. imageView.cornerRadius = 12
  217. return imageView
  218. }()
  219. lazy var checkView: UIView = {
  220. let bgView = UIView.creatColor(color: .black.withAlphaComponent(0.4))
  221. let checkImageView = UIImageView.createImageView(imageName: "ptp_selected_check")
  222. bgView.addSubview(checkImageView)
  223. checkImageView.snp.makeConstraints{ make in
  224. make.center.equalToSuperview()
  225. make.width.height.equalTo(32)
  226. }
  227. bgView.isHidden = true
  228. return bgView
  229. }()
  230. lazy var hotImageView: UIImageView = {
  231. let hotImageView = UIImageView.createImageView(imageName: "ptp_style_hot")
  232. hotImageView.isHidden = true
  233. return hotImageView
  234. }()
  235. lazy var textLabel: UILabel = {
  236. let textLabel = UILabel.createLabel(font: .font(size: 12),textColor: .white.withAlphaComponent(0.8),textAlignment: .center,numberOfLines: 2)
  237. return textLabel
  238. }()
  239. override func creatUI() {
  240. // let w = contentView.width
  241. let w = 78
  242. bgContentView.addSubview(boardImageView)
  243. boardImageView.snp.makeConstraints { make in
  244. make.top.centerX.equalToSuperview()
  245. make.width.height.equalTo(w)
  246. }
  247. bgContentView.addSubview(imageView)
  248. imageView.snp.makeConstraints { make in
  249. make.top.equalTo(4)
  250. make.centerX.equalToSuperview()
  251. make.width.height.equalTo(w-8)
  252. }
  253. imageView.addSubview(checkView)
  254. checkView.snp.makeConstraints { make in
  255. make.edges.equalToSuperview()
  256. }
  257. imageView.addSubview(hotImageView)
  258. hotImageView.snp.makeConstraints { make in
  259. make.leading.equalTo(0.0)
  260. make.top.equalTo(0.0)
  261. make.width.height.equalTo(36)
  262. }
  263. bgContentView.addSubview(textLabel)
  264. textLabel.snp.makeConstraints { make in
  265. make.top.equalTo(imageView.snp.bottom).offset(2)
  266. make.leading.trailing.bottom.equalToSuperview()
  267. }
  268. boardImageView.setNeedsLayout()
  269. DispatchQueue.main.async {
  270. 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))
  271. }
  272. }
  273. }