Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Jee] Step2 - 속성 변경 동작 #99

Open
wants to merge 15 commits into
base: Jee
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[ Create ] Delegate and Custom View
Jeeehee committed Mar 17, 2022
commit c9a3a9171bb2e8409f09337de67c1f4989244379
28 changes: 24 additions & 4 deletions DrawingApp/DrawingApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -9,7 +9,11 @@
/* Begin PBXBuildFile section */
73312E5027D618C10073D746 /* ID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 738A8A8227D5AC1400086985 /* ID.swift */; };
7348F21B27D7407F00613449 /* Plane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7348F21A27D7407F00613449 /* Plane.swift */; };
73583EE327E03E5800BE36DA /* RectengleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73583EE227E03E5800BE36DA /* RectengleView.swift */; };
735A316027DEE60900E3E4C0 /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 735A315F27DEE60900E3E4C0 /* Log.swift */; };
735A316327DF802500E3E4C0 /* Right View.xib in Resources */ = {isa = PBXBuildFile; fileRef = 735A316227DF802500E3E4C0 /* Right View.xib */; };
735A316727DF80ED00E3E4C0 /* RightView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 735A316627DF80ED00E3E4C0 /* RightView.swift */; };
735A316927DF84A600E3E4C0 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 735A316827DF84A600E3E4C0 /* ViewController.swift */; };
736D9D3B27CFBF6C00C040D8 /* DrawingAppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736D9D3A27CFBF6C00C040D8 /* DrawingAppTests.swift */; };
736D9D4227CFBF9600C040D8 /* Factory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73C750BC27CE1D1A00086C1D /* Factory.swift */; };
736D9D4327CFBF9E00C040D8 /* Point.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73C750B227CE00FE00086C1D /* Point.swift */; };
@@ -22,7 +26,6 @@
73966C8827DA10FE006883FF /* Convert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73966C8727DA10FE006883FF /* Convert.swift */; };
73C7509C27CDB7D300086C1D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73C7509B27CDB7D300086C1D /* AppDelegate.swift */; };
73C7509E27CDB7D300086C1D /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73C7509D27CDB7D300086C1D /* SceneDelegate.swift */; };
73C750A027CDB7D300086C1D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73C7509F27CDB7D300086C1D /* ViewController.swift */; };
73C750A327CDB7D300086C1D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 73C750A127CDB7D300086C1D /* Main.storyboard */; };
73C750A527CDB7D400086C1D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 73C750A427CDB7D400086C1D /* Assets.xcassets */; };
73C750A827CDB7D400086C1D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 73C750A627CDB7D400086C1D /* LaunchScreen.storyboard */; };
@@ -46,15 +49,18 @@

/* Begin PBXFileReference section */
7348F21A27D7407F00613449 /* Plane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Plane.swift; sourceTree = "<group>"; };
73583EE227E03E5800BE36DA /* RectengleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectengleView.swift; sourceTree = "<group>"; };
735A315F27DEE60900E3E4C0 /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
735A316227DF802500E3E4C0 /* Right View.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = "Right V\U0010iew.xib"; sourceTree = "<group>"; };
735A316627DF80ED00E3E4C0 /* RightView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RightView.swift; sourceTree = "<group>"; };
735A316827DF84A600E3E4C0 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
736D9D3827CFBF6C00C040D8 /* DrawingAppTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DrawingAppTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
736D9D3A27CFBF6C00C040D8 /* DrawingAppTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DrawingAppTests.swift; sourceTree = "<group>"; };
738A8A8227D5AC1400086985 /* ID.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ID.swift; sourceTree = "<group>"; };
73966C8727DA10FE006883FF /* Convert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Convert.swift; sourceTree = "<group>"; };
73C7509827CDB7D300086C1D /* DrawingApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DrawingApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
73C7509B27CDB7D300086C1D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
73C7509D27CDB7D300086C1D /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
73C7509F27CDB7D300086C1D /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
73C750A227CDB7D300086C1D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
73C750A427CDB7D400086C1D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
73C750A727CDB7D400086C1D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
@@ -93,6 +99,16 @@
path = Base;
sourceTree = "<group>";
};
735A316127DF7FFC00E3E4C0 /* View */ = {
isa = PBXGroup;
children = (
735A316227DF802500E3E4C0 /* Right View.xib */,
735A316627DF80ED00E3E4C0 /* RightView.swift */,
73583EE227E03E5800BE36DA /* RectengleView.swift */,
);
path = View;
sourceTree = "<group>";
};
736D9D3927CFBF6C00C040D8 /* DrawingAppTests */ = {
isa = PBXGroup;
children = (
@@ -116,7 +132,7 @@
738ADB1527DEE1A900BE418C /* VC */ = {
isa = PBXGroup;
children = (
73C7509F27CDB7D300086C1D /* ViewController.swift */,
735A316827DF84A600E3E4C0 /* ViewController.swift */,
);
path = VC;
sourceTree = "<group>";
@@ -128,6 +144,7 @@
73C750BC27CE1D1A00086C1D /* Factory.swift */,
735A315F27DEE60900E3E4C0 /* Log.swift */,
73C750AF27CE003D00086C1D /* Model */,
735A316127DF7FFC00E3E4C0 /* View */,
738ADB1527DEE1A900BE418C /* VC */,
);
path = Sources;
@@ -264,6 +281,7 @@
buildActionMask = 2147483647;
files = (
73C750A827CDB7D400086C1D /* LaunchScreen.storyboard in Resources */,
735A316327DF802500E3E4C0 /* Right View.xib in Resources */,
73C750A527CDB7D400086C1D /* Assets.xcassets in Resources */,
73C750A327CDB7D300086C1D /* Main.storyboard in Resources */,
);
@@ -292,13 +310,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
735A316927DF84A600E3E4C0 /* ViewController.swift in Sources */,
73C750B127CE007300086C1D /* Size.swift in Sources */,
735A316027DEE60900E3E4C0 /* Log.swift in Sources */,
738A8A8327D5AC1400086985 /* ID.swift in Sources */,
73C750B527CE015800086C1D /* Color.swift in Sources */,
73C750B727CE01CF00086C1D /* Alpha.swift in Sources */,
73C750B327CE00FE00086C1D /* Point.swift in Sources */,
73C750A027CDB7D300086C1D /* ViewController.swift in Sources */,
73583EE327E03E5800BE36DA /* RectengleView.swift in Sources */,
735A316727DF80ED00E3E4C0 /* RightView.swift in Sources */,
73C750BD27CE1D1A00086C1D /* Factory.swift in Sources */,
73966C8827DA10FE006883FF /* Convert.swift in Sources */,
73C7509C27CDB7D300086C1D /* AppDelegate.swift in Sources */,
8 changes: 8 additions & 0 deletions DrawingApp/DrawingApp/Resources/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -16,6 +16,11 @@
<rect key="frame" x="0.0" y="0.0" width="1180" height="820"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="rUP-ih-w6b">
<rect key="frame" x="0.0" y="0.0" width="800" height="820"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</view>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="zfB-M5-mqc">
<rect key="frame" x="480" y="740" width="221" height="80"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
@@ -29,6 +34,9 @@
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</view>
<connections>
<outlet property="canvas" destination="rUP-ih-w6b" id="4le-Cu-zUv"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
4 changes: 2 additions & 2 deletions DrawingApp/DrawingApp/Sources/Convert.swift
Original file line number Diff line number Diff line change
@@ -27,14 +27,14 @@ extension UIColor {

func toHex() -> String? {
guard let components = cgColor.components, components.count >= 3 else {
return nil
return nil // components가 [CGFloat]? 여서 옵셔널 바인딩, 최소 3개 이상
}

let r = Float(components[0])
let g = Float(components[1])
let b = Float(components[2])

return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)) //
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UIColor.toHex()처럼 위에 Convert.toUIColor()도 UIColor를 확장해도 됩니다. 결국 UIColor 인스턴스를 만드는 거니까요.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UIColor를 확장 -> convenience init 을 활용해 수정했습니다!
convenience init을 이렇게도 사용할 수 있었다니..! 다양한 방법을 알려주셔서 감사합니다 :D


}
12 changes: 3 additions & 9 deletions DrawingApp/DrawingApp/Sources/Factory.swift
Original file line number Diff line number Diff line change
@@ -10,17 +10,11 @@ import UIKit

class Factory {

static func createRectangle() -> Rectangle {

let marginX = 130.0
let marginY = 160.0

let DeviceWidth = UIScreen.main.bounds.size.width - marginX
let DeviceHeight = UIScreen.main.bounds.size.height - marginY
static func createRectangle(marginX: Double, marginY: Double) -> Rectangle {

let size = Size(width: 150, height: 120)
let point = Point(x: round(Double.random(in: 0..<DeviceWidth)),
y: round(Double.random(in: 0..<DeviceHeight)))
let point = Point(x: round(Double.random(in: 0..<marginX)),
y: round(Double.random(in: 0..<marginY)))
let color = Color(r: round(Double.random(in: 0...255)),
g: round(Double.random(in: 0...255)),
b: round(Double.random(in: 0...255)))
4 changes: 2 additions & 2 deletions DrawingApp/DrawingApp/Sources/Model/Plane.swift
Original file line number Diff line number Diff line change
@@ -16,8 +16,8 @@ class Plane {
return rectangles[index]
}

func addRectangle() -> Rectangle {
let add = Factory.createRectangle()
func createRectangle(marginX: Double, marginY: Double) -> Rectangle {
let add = Factory.createRectangle(marginX: marginX, marginY: marginY)
rectangles.append(add)
return add
}
6 changes: 5 additions & 1 deletion DrawingApp/DrawingApp/Sources/Model/Rectangle.swift
Original file line number Diff line number Diff line change
@@ -30,8 +30,12 @@ extension Rectangle: CustomStringConvertible {
}
}

extension Rectangle: Equatable {
extension Rectangle: Equatable, Hashable {
static func == (lhs: Rectangle, rhs: Rectangle) -> Bool {
return lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
13 changes: 13 additions & 0 deletions DrawingApp/DrawingApp/Sources/PlaneDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// PlaneDelegate.swift
// DrawingApp
//
// Created by Jihee hwang on 2022/03/15.
//

import Foundation

protocol PlaneDelegate {
func didCreateRectengle()
func didSelect()
}
27 changes: 12 additions & 15 deletions DrawingApp/DrawingApp/Sources/VC/ViewController.swift
Original file line number Diff line number Diff line change
@@ -9,28 +9,25 @@ import UIKit
import OSLog

class ViewController: UIViewController {


private var model = Plane()
private var rectengles = [Rectangle: RectengleView]() // 이 안에 Rectangle 값이랑, 그에 맞는 view를 넣어

@IBOutlet weak var canvas: UIView!

override func viewDidLoad() {
super.viewDidLoad()

}


@IBAction func createRectangle(_ sender: UIButton) {
let newRectangle = Factory.createRectangle()
let rectangle = model.createRectangle(marginX: canvas.bounds.width, marginY: canvas.bounds.height)

OSLog.log(message: newRectangle.description)

let test = UIView.init(frame: CGRect(x: newRectangle.point.x, y: newRectangle.point.y, width: newRectangle.size.width, height: newRectangle.size.height))
let view = RectengleView.init(frame: CGRect(x: rectangle.point.x, y: rectangle.point.y, width: rectangle.size.width, height: rectangle.size.height))

let check = Convert.toUIColor(color: newRectangle.color, alpha: newRectangle.alpha)

test.backgroundColor = check
print(check.toHex() ?? "")



self.view.addSubview(test)

self.canvas.addSubview(view)
}


}

39 changes: 39 additions & 0 deletions DrawingApp/DrawingApp/Sources/View/RectengleView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// RectengleView.swift
// DrawingApp
//
// Created by Jihee hwang on 2022/03/15.
//

import UIKit

class RectengleView: UIView {

override init(frame: CGRect) {
super.init(frame: frame)
}

required init?(coder: NSCoder) {
super.init(coder: coder)
}

convenience init(frame: CGRect, color: Color, alpha: Alpha) {
self.init(frame: frame)
setColor(color: color, alpha: alpha)
}

func setColor(color: Color, alpha: Alpha) {
self.backgroundColor = UIColor(red: color.r, green: color.g, blue: color.b, alpha: alpha.value)
}

func selectRectangle(isSelect: Bool) { // 선택된 사각형 테두리
if isSelect == true {
self.layer.borderColor = UIColor.black.cgColor
self.layer.borderWidth = 5
} else {
self.layer.borderColor = UIColor.clear.cgColor
self.layer.borderWidth = 0
}
}

}
18 changes: 18 additions & 0 deletions DrawingApp/DrawingApp/Sources/View/Right View.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13142" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12042"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
</view>
</objects>
</document>
36 changes: 36 additions & 0 deletions DrawingApp/DrawingApp/Sources/View/RightView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// RightView.swift
// DrawingApp
//
// Created by Jihee hwang on 2022/03/14.
//

import UIKit


//@IBDesignable
class RightView: UIView {

@IBOutlet weak var rightViewBackground: UIView!

required init?(coder: NSCoder) {
super.init(coder: coder)
loadXib()
}

override init(frame: CGRect) {
super.init(frame: frame)
loadXib()
}

private func loadXib() {
let identifier = String(describing: type(of: self))
let nibs = Bundle.main.loadNibNamed(identifier, owner: self, options: nil)
guard let inputView = nibs?.first as? UIView else {
return
}
inputView.frame = self.bounds
self.addSubview(inputView)
}

}