Files
StickerSlack/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift
T
2026-04-30 12:23:53 +01:00

115 lines
3.0 KiB
Swift

//
// EmojiCollectionViewCell.swift
// StickerSlack
//
// Created by neon443 on 26/04/2026.
//
import Foundation
import UIKit
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)
label.font = .preferredFont(forTextStyle: .caption1)
label.textAlignment = .center
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)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func configureSkeleton() {
super.configureSkeleton()
}
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() {
super.prepareForReuse()
label.text = nil
}
}