Skip to content

Commit 986251e

Browse files
authored
Merge pull request #24 from kut7728/feature/#19
PinEditViewController 코드 정리
2 parents d72a2dc + cf5b9af commit 986251e

File tree

1 file changed

+99
-91
lines changed

1 file changed

+99
-91
lines changed

Pinit/Pinit/Views/PinEdit/PinEditViewController.swift

Lines changed: 99 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -9,77 +9,63 @@ import UIKit
99
import MapKit
1010
import SnapKit
1111

12+
enum PinMode {
13+
case create(latitude: Double, longtitude: Double)
14+
case edit
15+
}
16+
1217
final class PinEditViewController: UIViewController, UITextViewDelegate {
1318

14-
private var mapView: MKMapView! //mapview 불러옴
15-
private let datebutton = UIButton() // leftbutton을 클래스 프로퍼티로 변경
19+
private var mapView: MKMapView!
20+
private let pinmode: PinMode = .edit
1621

1722
override func viewDidLoad() {
1823
super.viewDidLoad()
19-
self.view.backgroundColor = .white
2024

25+
view.backgroundColor = .white
26+
27+
switch pinmode {
28+
case let .create(latitude, longtitude):
29+
print("\(latitude), \(longtitude)")
30+
case .edit:
31+
print("편집 모드입니다")
32+
}
33+
34+
//MARK: mapView 설정
2135
mapView = MKMapView(frame: self.view.bounds) //mkmapview 초기화 및 뷰 추가함
2236
mapView = MKMapView(frame: CGRect(x: 0, y: 60, width: self.view.bounds.width, height: self.view.bounds.height / 4)) //화면의 1/4만 나오게 함
23-
2437
self.view.addSubview(mapView) //mapview 뷰에 보이게
38+
2539
setUpKeyboard()
2640

2741
let center = CLLocationCoordinate2D(latitude: 37.506446, longitude: 126.885397) //중심좌표
2842
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
2943
mapView.setRegion(region, animated: true)
3044

3145

32-
// 지도 오른쪽 위에 닫기 버튼 추가, xmark.circle.fill
33-
let closeButton = UIButton(type: .system)
46+
//MARK: 지도 오른쪽 위에 닫기 버튼 추가, xmark.circle.fill
3447
if let closeImage = UIImage(systemName: "xmark.circle.fill") {
3548
let largeConfig = UIImage.SymbolConfiguration(pointSize: 45, weight: .bold, scale: .default)
3649
let largeImage = closeImage.withConfiguration(largeConfig)
3750
closeButton.setImage(largeImage, for: .normal)
3851
}
3952
closeButton.tintColor = .black
4053
closeButton.alpha = 0.7 // 투명도 50% 설정
54+
closeButton.addTarget(self, action: #selector(dismissButtonTapped), for: .touchUpInside)
4155

42-
self.view.addSubview(closeButton)
43-
44-
// Auto Layout 설정
45-
closeButton.snp.makeConstraints {
46-
$0.top.equalToSuperview().offset(65) // 상단에서 65포인트
47-
$0.trailing.equalToSuperview().offset(-5) // 오른쪽에서 5
48-
$0.width.height.equalTo(40) // 버튼 크기
49-
}
50-
51-
//왼쪽 기록 날짜 버튼
56+
//MARK: 왼쪽 기록 날짜 버튼
5257
datebutton.backgroundColor = .clear
5358
datebutton.layer.cornerRadius = 10
5459
datebutton.setTitle("3월 17일", for: .normal) //for: .normal은 아무런 상호작용 없을때 상태
55-
self.view.addSubview(datebutton) //뷰에 leftbutton 보여줌
5660
datebutton.setTitleColor(UIColor.black, for: .normal) // 글자색을 검은색으로 변경
5761

58-
//오토레이아웃 설정
59-
datebutton.snp.makeConstraints{
60-
$0.leading.equalToSuperview().offset(10) //왼쪽에서 10 떨어짐
61-
$0.top.equalToSuperview().offset(300) //탑에서 300
62-
$0.width.equalTo(180) //너비 200
63-
$0.height.equalTo(30) //높이 30
64-
}
6562

66-
//오른쪽 날씨 버튼
67-
let weatherbutton = UIButton()
63+
//MARK: 오른쪽 날씨 버튼
6864
weatherbutton.layer.cornerRadius = 10
6965
weatherbutton.setTitle("맑음", for: .normal)
7066
weatherbutton.setTitleColor(UIColor.black, for: .normal) // 글자색을 검은색으로 변경
71-
self.view.addSubview(weatherbutton)
72-
73-
//오토레이아웃 설정
74-
weatherbutton.snp.makeConstraints{
75-
$0.leading.equalToSuperview().offset(210)
76-
$0.top.equalToSuperview().offset(300)
77-
$0.width.equalTo(180)
78-
$0.height.equalTo(30)
79-
}
80-
81-
//그 아래 카메라 버튼
82-
let camerabutton = UIButton()
67+
68+
//MARK: 아래 카메라 버튼
8369
camerabutton.backgroundColor = .white
8470
camerabutton.layer.cornerRadius = 75
8571
camerabutton.layer.shadowColor = UIColor.black.cgColor // 색깔
@@ -97,22 +83,7 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
9783

9884
camerabutton.addTarget(self, action: #selector(cameraButtonTapped), for: .touchUpInside)
9985

100-
self.view.addSubview(camerabutton)
101-
//사진 배경 흰색, 안에 아이콘을 검은색
102-
103-
// Auto Layout 설정
104-
camerabutton.snp.makeConstraints{
105-
$0.centerX.equalToSuperview()
106-
$0.centerY.equalToSuperview()
107-
$0.width.equalTo(150)
108-
$0.height.equalTo(150)
109-
110-
}
111-
112-
// 배경 흰색, stroke를 회색
113-
// 제목 작성 버튼
114-
let titlefield = UITextField()
115-
titlefield.backgroundColor = .white
86+
//MARK: 제목 작성 버튼
11687
titlefield.layer.borderColor = UIColor(red: 169/255, green: 169/255, blue: 169/255, alpha: 1).cgColor // #A9A9A9 (다크 라이트 그레이)
11788
titlefield.layer.borderWidth = 2 // 테두리 두께 설정
11889
titlefield.textColor = .black
@@ -126,17 +97,7 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
12697
titlefield.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: 0)) // 이거 개쩜 제목 작성 맨 앞에 여백을 주는거임
12798
titlefield.leftViewMode = .always //항상
12899

129-
self.view.addSubview(titlefield)
130-
131-
titlefield.snp.makeConstraints{
132-
$0.leading.equalToSuperview().offset(20)
133-
$0.top.equalToSuperview().offset(540)
134-
$0.width.equalTo(360)
135-
$0.height.equalTo(40)
136-
}
137-
138-
// 추가메모 텍스트뷰 추가
139-
let contentTextView = UITextView()
100+
//MARK: 추가메모 텍스트뷰 추가
140101
contentTextView.backgroundColor = .white
141102
contentTextView.layer.borderColor = UIColor(red: 169/255, green: 169/255, blue: 169/255, alpha: 1).cgColor // #A9A9A9 (다크 라이트 그레이)
142103
contentTextView.layer.borderWidth = 2 // 테두리 두께 설정
@@ -145,34 +106,17 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
145106
contentTextView.textAlignment = .center
146107
contentTextView.textColor = UIColor.black
147108
contentTextView.font = UIFont.systemFont(ofSize: 16)
148-
self.view.addSubview(contentTextView)
149-
150-
contentTextView.snp.makeConstraints {
151-
$0.leading.equalToSuperview().offset(20)
152-
$0.top.equalTo(titlefield.snp.bottom).offset(10)
153-
$0.width.equalTo(360)
154-
$0.height.equalTo(150)
155-
}
156109

157110
contentTextView.delegate = self
158111

159-
//저장 버튼
160-
let savebutton = UIButton()
112+
//MARK: 저장 버튼
161113
savebutton.backgroundColor = UIColor(red: 28/255, green: 70/255, blue: 245/255, alpha: 1) // #FF8C42 (딥 오렌지)
162114
savebutton.setTitle("저장", for: .normal)
163115
savebutton.layer.cornerRadius = 10
164-
self.view.addSubview(savebutton)
165-
166-
//오토레이아웃 설정
167-
savebutton.snp.makeConstraints{
168-
$0.leading.equalToSuperview().offset(130)
169-
$0.top.equalToSuperview().offset(760)
170-
$0.width.equalTo(150)
171-
$0.height.equalTo(55)
172-
}
116+
setui()
173117

174118

175-
//키보드 완료 버튼
119+
//MARK: 키보드 완료 버튼
176120
let keyboardToolbar = UIToolbar()
177121
let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
178122
let doneBarButton = UIBarButtonItem(title: "완료", style: .plain, target: self, action: #selector(doneBtnClicked))
@@ -184,28 +128,82 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
184128

185129
}
186130

131+
//MARK: 오토레이아웃 정리
132+
let savebutton = UIButton()
133+
let contentTextView = UITextView()
134+
let titlefield = UITextField()
135+
let camerabutton = UIButton()
136+
let weatherbutton = UIButton()
137+
private let datebutton = UIButton()
138+
let closeButton = UIButton(type: .system)
139+
func setui() {
140+
self.view.addSubviews(savebutton, contentTextView, titlefield, camerabutton, weatherbutton, datebutton, closeButton)
141+
142+
savebutton.snp.makeConstraints{
143+
$0.leading.equalToSuperview().offset(130)
144+
$0.top.equalToSuperview().offset(760)
145+
$0.width.equalTo(150)
146+
$0.height.equalTo(55)
147+
}
148+
contentTextView.snp.makeConstraints {
149+
$0.leading.equalToSuperview().offset(20)
150+
$0.top.equalTo(titlefield.snp.bottom).offset(10)
151+
$0.width.equalTo(360)
152+
$0.height.equalTo(150)
153+
}
154+
titlefield.snp.makeConstraints{
155+
$0.leading.equalToSuperview().offset(20)
156+
$0.top.equalToSuperview().offset(540)
157+
$0.width.equalTo(360)
158+
$0.height.equalTo(40)
159+
}
160+
camerabutton.snp.makeConstraints{
161+
$0.centerX.equalToSuperview()
162+
$0.centerY.equalToSuperview()
163+
$0.width.equalTo(150)
164+
$0.height.equalTo(150)
165+
}
166+
weatherbutton.snp.makeConstraints{
167+
$0.leading.equalToSuperview().offset(210)
168+
$0.top.equalToSuperview().offset(300)
169+
$0.width.equalTo(180)
170+
$0.height.equalTo(30)
171+
}
172+
datebutton.snp.makeConstraints{
173+
$0.leading.equalToSuperview().offset(10)
174+
$0.top.equalToSuperview().offset(300)
175+
$0.width.equalTo(180)
176+
$0.height.equalTo(30)
177+
}
178+
closeButton.snp.makeConstraints {
179+
$0.top.equalToSuperview().offset(65)
180+
$0.trailing.equalToSuperview().offset(-5)
181+
$0.width.height.equalTo(40)
182+
}
183+
}
184+
185+
//MARK: 키보드 닫기
187186
@objc func doneBtnClicked() {
188-
view.endEditing(true) // 키보드 닫기
187+
view.endEditing(true)
189188
}
190189

190+
//MARK: 카메라 선택, 사진 선택
191191
@objc func cameraButtonTapped() {
192192
let actionSheet = UIAlertController(title: "사진 선택", message: "사진을 가져올 방법을 선택하세요.", preferredStyle: .actionSheet)
193-
194193
let cameraAction = UIAlertAction(title: "카메라", style: .default) { _ in
195194
self.presentImagePicker(sourceType: .camera)
196195
}
197196
let galleryAction = UIAlertAction(title: "갤러리", style: .default) { _ in
198197
self.presentImagePicker(sourceType: .photoLibrary)
199198
}
200199
let cancelAction = UIAlertAction(title: "취소", style: .cancel, handler: nil)
201-
202200
actionSheet.addAction(cameraAction)
203201
actionSheet.addAction(galleryAction)
204202
actionSheet.addAction(cancelAction)
205-
206203
present(actionSheet, animated: true, completion: nil)
207204
}
208205

206+
//MARK: 사진 선택하면 화면에 사진 띄우기
209207
func presentImagePicker(sourceType: UIImagePickerController.SourceType) {
210208
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else { return }
211209

@@ -217,6 +215,7 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
217215
present(imagePicker, animated: true, completion: nil)
218216
}
219217

218+
//MARK: 시스템에 알리는 키보드 상태 알림
220219
func setUpKeyboard() {
221220
NotificationCenter.default.addObserver(self,
222221
selector: #selector(keyboardWillShow),
@@ -228,21 +227,29 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
228227
object: nil)
229228
}
230229

230+
@objc func dismissButtonTapped() {
231+
self.dismiss(animated: true)
232+
}
233+
234+
//MARK: 키보드가 나타낼때 화면을 -300
231235
@objc func keyboardWillShow(notification: NSNotification) {
232-
view.frame.origin.y = -300 //키보드가 나타날 때 동작
236+
view.frame.origin.y = -300
233237
}
234238

239+
//MARK: 키보드가 사라질 때 동작
235240
@objc func keyboardWillHide(notification: NSNotification) {
236-
view.frame.origin.y = 0 //키보드가 사라질 때 동작
241+
view.frame.origin.y = 0
237242
}
238243

244+
//MARK: 사용자가 텍스트뷰에 입력을 시작할 때 기본 안내 문구
239245
func textViewDidBeginEditing(_ textView: UITextView) {
240246
if textView.text == "남기고자 하는 메모가 있다면 작성해주세요." {
241247
textView.text = ""
242248
textView.textColor = .black
243249
}
244250
}
245251

252+
//MARK: 텍스트뷰가 비어있을 때 안내 메시지를 다시 표시
246253
func textViewDidEndEditing(_ textView: UITextView) {
247254
if textView.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
248255
textView.text = "남기고자 하는 메모가 있다면 작성해주세요."
@@ -251,6 +258,7 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
251258
}
252259
}
253260

261+
//MARK: PinEditViewController 내에서 사진 선택 기능을 쉽게 사용
254262
extension PinEditViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
255263
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
256264
if let selectedImage = info[.editedImage] as? UIImage ?? info[.originalImage] as? UIImage {

0 commit comments

Comments
 (0)