From 56853e9cff13f6f95ff1e0e4392d1f0d523ffcb2 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Thu, 30 Apr 2026 12:23:53 +0100 Subject: [PATCH] fix buttons again --- .../CollectionView/EmojiCollectionView.swift | 51 +++------------ .../EmojiCollectionViewCell.swift | 62 +++++++++++++++++++ 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift index d94d38b..c8ee647 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift @@ -30,12 +30,19 @@ struct EmojiCollectionView: UIViewRepresentable { func updateUIView(_ uiView: UICollectionView, context: Context) { context.coordinator.hoarder = hoarder + context.coordinator.edit = edit if edit { context.coordinator.startAnimating() } else { context.coordinator.stopAnimating() } + for cell in uiView.visibleCells { + if let cell = cell as? EmojiCollectionViewCell { + cell.setEdit(to: edit) + } + } + context.coordinator.onRemove = onRemove if items != context.coordinator.items || context.coordinator.pack != pack { context.coordinator.items = items @@ -123,25 +130,6 @@ struct EmojiCollectionView: UIViewRepresentable { cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell case .full: cell = collectionView.dequeueReusableCell(withReuseIdentifier: "full", for: indexPath) as! EmojiCollectionViewCell - (cell as! EmojiCollectionViewCell).setEdit(to: edit) - - let button = UIButton(type: .custom) - button.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal) - button.imageView?.contentMode = .scaleAspectFit - button.contentHorizontalAlignment = .fill - button.contentVerticalAlignment = .fill - button.addTarget(self, action: #selector(deleteTapped), for: .touchUpInside) - button.tintColor = .systemRed - button.clipsToBounds = false - button.translatesAutoresizingMaskIntoConstraints = false - collectionView.addSubview(button) - - NSLayoutConstraint.activate([ - button.topAnchor.constraint(equalTo: cell.topAnchor, constant: -12), - button.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: -12), - button.heightAnchor.constraint(equalToConstant: 24), - button.widthAnchor.constraint(equalToConstant: 24) - ]) } guard !hoarder.trie.dict.isEmpty else { return cell } @@ -152,16 +140,10 @@ struct EmojiCollectionView: UIViewRepresentable { return cell } - @objc - func deleteTapped() { - print(UUID()) - } - func startAnimating() { displayLink = CADisplayLink(target: self, selector: #selector(update)) displayLink.add(to: .main, forMode: .common) } - func stopAnimating() { displayLink?.invalidate() UIView.animate(withDuration: 0.2) { @@ -170,7 +152,6 @@ struct EmojiCollectionView: UIViewRepresentable { } } } - @objc func update() { let t = Date().timeIntervalSince(initDate) @@ -181,24 +162,6 @@ struct EmojiCollectionView: UIViewRepresentable { } } -// func spinAnimation() { -// guard edit else { -// UIView.animate(withDuration: 0.1, delay: 0, options: .curveEaseInOut) { -// for cell in self.collectionView.visibleCells { -// cell.transform = CGAffineTransform.identity -// } -// } -// return -// } -// UIView.animate(withDuration: 0.1, delay: 0, options: [.curveLinear]) { -// for cell in self.collectionView.visibleCells { -// cell.transform = CGAffineTransform(rotationAngle: ((sin(self.initDate.timeIntervalSinceNow*20)*4)/360)*2*CGFloat.pi) -// } -// } completion: { _ in -// self.spinAnimation() -// } -// } - func collectionView( _ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift index 05ffa62..8ce4e0b 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift @@ -11,7 +11,9 @@ import SwiftUI class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { let label = UILabel() + let button = UIButton(type: .custom) var edit: Bool = false + var onDelete: ((String) -> Void)? override init(frame: CGRect) { super.init(frame: frame) @@ -21,10 +23,27 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { label.numberOfLines = 0 label.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(label) + + button.alpha = 0 + button.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal) + button.imageView?.contentMode = .scaleAspectFit + button.contentHorizontalAlignment = .fill + button.contentVerticalAlignment = .fill + button.addTarget(self, action: #selector(deleteTapped), for: .touchUpInside) + button.tintColor = .systemRed + button.clipsToBounds = false + button.translatesAutoresizingMaskIntoConstraints = false + contentView.addSubview(button) + NSLayoutConstraint.activate([ label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), + + button.topAnchor.constraint(equalTo: contentView.topAnchor, constant: -12), + button.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: -12), + button.heightAnchor.constraint(equalToConstant: 24), + button.widthAnchor.constraint(equalToConstant: 24) ]) } @@ -39,10 +58,53 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { override func configure(with: EmojiHoarder, emoji: Emoji) { super.configure(with: with, emoji: emoji) label.text = emoji.UIName + contentView.bringSubviewToFront(button) } func setEdit(to newValue: Bool) { self.edit = newValue + if edit { + contentView.bringSubviewToFront(button) + self.button.isHidden = false + UIView.animate(withDuration: 0.2) { + self.button.alpha = 1 + } completion: { _ in + } + } else { +// UIView.animate(withDuration: 0.1) { +// self.button.transform = CGAffineTransform.identity +// } completion: { _ in + UIView.animate(withDuration: 0.2) { + self.button.alpha = 0 + } completion: { _ in + self.button.isHidden = true + } +// } + } + } + + func setOnDelete(to newValue: ((String) -> Void)?) { + self.onDelete = newValue + } + + @objc + func deleteTapped() { + onDelete?("") + print(UUID()) + } + + override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { + let expandedBounds = bounds.insetBy( + dx: -button.bounds.width/2, + dy: -button.bounds.height/2, + ) + if expandedBounds.contains(point) { + let buttonPoint = convert(point, to: button) + if button.bounds.contains(buttonPoint) { + return button + } + } + return super.hitTest(point, with: event) } override func prepareForReuse() {