fix buttons again

This commit is contained in:
neon443 committed 2026-04-30 12:23:53 +01:00
1 parent 0cfd8e47bb
commit 56853e9cff
2 files changed
+69 -44

No files matched your search

@@ -30,12 +30,19 @@ struct EmojiCollectionView: UIViewRepresentable {
func updateUIView(_ uiView: UICollectionView, context: Context) { func updateUIView(_ uiView: UICollectionView, context: Context) {
context.coordinator.hoarder = hoarder context.coordinator.hoarder = hoarder
context.coordinator.edit = edit context.coordinator.edit = edit
if edit { if edit {
context.coordinator.startAnimating() context.coordinator.startAnimating()
} else { } else {
context.coordinator.stopAnimating() context.coordinator.stopAnimating()
} }
for cell in uiView.visibleCells {
if let cell = cell as? EmojiCollectionViewCell {
cell.setEdit(to: edit)
}
}
context.coordinator.onRemove = onRemove context.coordinator.onRemove = onRemove
if items != context.coordinator.items || context.coordinator.pack != pack { if items != context.coordinator.items || context.coordinator.pack != pack {
context.coordinator.items = items context.coordinator.items = items
@@ -123,25 +130,6 @@ struct EmojiCollectionView: UIViewRepresentable {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell
case .full: case .full:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "full", for: indexPath) as! EmojiCollectionViewCell 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 } guard !hoarder.trie.dict.isEmpty else { return cell }
@@ -152,16 +140,10 @@ struct EmojiCollectionView: UIViewRepresentable {
return cell return cell
} }
@objc
func deleteTapped() {
print(UUID())
}
func startAnimating() { func startAnimating() {
displayLink = CADisplayLink(target: self, selector: #selector(update)) displayLink = CADisplayLink(target: self, selector: #selector(update))
displayLink.add(to: .main, forMode: .common) displayLink.add(to: .main, forMode: .common)
} }
func stopAnimating() { func stopAnimating() {
displayLink?.invalidate() displayLink?.invalidate()
UIView.animate(withDuration: 0.2) { UIView.animate(withDuration: 0.2) {
@@ -170,7 +152,6 @@ struct EmojiCollectionView: UIViewRepresentable {
} }
} }
} }
@objc @objc
func update() { func update() {
let t = Date().timeIntervalSince(initDate) 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( func collectionView(
_ collectionView: UICollectionView, _ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout, layout collectionViewLayout: UICollectionViewLayout,
@@ -11,7 +11,9 @@ import SwiftUI
class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
let label = UILabel() let label = UILabel()
let button = UIButton(type: .custom)
var edit: Bool = false var edit: Bool = false
var onDelete: ((String) -> Void)?
override init(frame: CGRect) { override init(frame: CGRect) {
super.init(frame: frame) super.init(frame: frame)
@@ -21,10 +23,27 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
label.numberOfLines = 0 label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false label.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(label) 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([ NSLayoutConstraint.activate([
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), 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) { override func configure(with: EmojiHoarder, emoji: Emoji) {
super.configure(with: with, emoji: emoji) super.configure(with: with, emoji: emoji)
label.text = emoji.UIName label.text = emoji.UIName
contentView.bringSubviewToFront(button)
} }
func setEdit(to newValue: Bool) { func setEdit(to newValue: Bool) {
self.edit = newValue 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() { override func prepareForReuse() {
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.