-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : 선택된 사진들을 좌우 스와이프가 가능한 CollectionView 로 생성 #7
- SelectedPhotoCell 생성 - 스와이프 페이지 인덱싱 적용 - 네비게이션바에 "다음" 버튼 추가
- Loading branch information
Showing
2 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
Particle/Particle/Main/HomeTab/AddArticle/SelectSentence/View/SelectedPhotoCell.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// | ||
// SelectedPhotoCell.swift | ||
// Particle | ||
// | ||
// Created by 이원빈 on 2023/07/30. | ||
// | ||
|
||
import UIKit | ||
import Photos | ||
import VisionKit | ||
|
||
protocol SelectedPhotoCellListener: AnyObject { | ||
func copyButtonTapped(with text: String) | ||
} | ||
|
||
final class SelectedPhotoCell: UICollectionViewCell { | ||
|
||
private let mainScrollView: UIScrollView = { | ||
let scrollView = UIScrollView() | ||
return scrollView | ||
}() | ||
|
||
private let imageView: UIImageView = { | ||
let imageView = UIImageView() | ||
imageView.contentMode = .scaleAspectFit | ||
return imageView | ||
}() | ||
|
||
private lazy var interaction: ImageAnalysisInteraction = { | ||
let interaction = ImageAnalysisInteraction() | ||
interaction.preferredInteractionTypes = .automatic | ||
interaction.allowLongPressForDataDetectorsInTextMode = true | ||
return interaction | ||
}() | ||
|
||
private let imageAnalyzer = ImageAnalyzer() | ||
|
||
private var copiedText = "" | ||
weak var listener: SelectedPhotoCellListener? | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
addSubviews() | ||
setConstraints() | ||
contentView.clipsToBounds = true | ||
NotificationCenter.default.addObserver( | ||
self, | ||
selector: #selector(copyButtonTapped), | ||
name: UIPasteboard.changedNotification, | ||
object: nil | ||
) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
deinit { | ||
NotificationCenter.default.removeObserver(self) | ||
} | ||
|
||
override func prepareForReuse() { | ||
super.prepareForReuse() | ||
imageView.image = nil | ||
} | ||
|
||
private func showLiveText() { | ||
guard let image = imageView.image else { | ||
Console.error("imageView.image == nil 입니다.") | ||
return | ||
} | ||
|
||
Task { | ||
let configuration = ImageAnalyzer.Configuration([.text]) | ||
|
||
do { | ||
let analysis = try await imageAnalyzer.analyze(image, configuration: configuration) | ||
|
||
DispatchQueue.main.async { | ||
self.interaction.analysis = nil | ||
self.interaction.preferredInteractionTypes = [] | ||
|
||
self.interaction.analysis = analysis | ||
self.interaction.preferredInteractionTypes = .textSelection | ||
} | ||
} catch { | ||
Console.error(error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
|
||
@objc func copyButtonTapped() { | ||
if let theString = UIPasteboard.general.string { | ||
copiedText = theString | ||
Console.log(copiedText) | ||
listener?.copyButtonTapped(with: copiedText) | ||
} | ||
} | ||
|
||
func setImage(with asset: PHAsset) { | ||
imageView.addInteraction(interaction) | ||
|
||
imageView.fetchImage( | ||
asset: asset, | ||
contentMode: .default, | ||
targetSize: imageView.frame.size | ||
) { [weak self] aspectRatio in | ||
self?.imageView.snp.makeConstraints { | ||
$0.width.equalTo(DeviceSize.width) | ||
$0.height.equalTo(aspectRatio * DeviceSize.width) | ||
} | ||
self?.showLiveText() | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Layout Settting | ||
|
||
private extension SelectedPhotoCell { | ||
|
||
func addSubviews() { | ||
contentView.addSubview(mainScrollView) | ||
mainScrollView.addSubview(imageView) | ||
} | ||
|
||
func setConstraints() { | ||
mainScrollView.snp.makeConstraints { | ||
$0.top.bottom.leading.trailing.equalTo(contentView.safeAreaLayoutGuide) | ||
} | ||
|
||
imageView.snp.makeConstraints { | ||
$0.top.bottom.leading.trailing.equalTo(mainScrollView) | ||
} | ||
} | ||
} |