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

스티커 렌더링 수정 #74

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
78 changes: 78 additions & 0 deletions 90s/90s.xcodeproj/xcshareddata/xcschemes/90s.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1320"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9BB7612B254EACF4005B7B5A"
BuildableName = "90s.app"
BlueprintName = "90s"
ReferencedContainer = "container:90s.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9BB7612B254EACF4005B7B5A"
BuildableName = "90s.app"
BlueprintName = "90s"
ReferencedContainer = "container:90s.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9BB7612B254EACF4005B7B5A"
BuildableName = "90s.app"
BlueprintName = "90s"
ReferencedContainer = "container:90s.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "test_pic5.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,54 @@

import UIKit

class PhotoDecorateViewController: BaseViewController {
class PhotoDecorateViewController: BaseViewController, UIScrollViewDelegate {

private struct Constraints {
static let photoInset: UIEdgeInsets = .init(top: 40, left: 40, bottom: 40, right: 40)
static let photoInset: UIEdgeInsets = .init(top: 20, left: 20, bottom: 20, right: 20)
}

private var decorator = StickerDecorator()

// MARK: - Views

private(set) var decoratingView: UIView = {
private lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.minimumZoomScale = self.minimumZoomScale
scrollView.maximumZoomScale = self.maximumZoomScale
scrollView.bouncesZoom = false
scrollView.delegate = self
return scrollView
}()

private let contentView: UIView = {
let view = UIView()

return view
}()

private(set) var decoratingBorderView: UIView = {
private let decoratingAreaView: UIView = {
let view = UIView()
view.layer.borderColor = UIColor.white.cgColor
view.layer.borderWidth = 1

return view
}()

private let decoratingView: UIView = {
let view = UIView()
view.backgroundColor = .clear

return view
}()

private let decoratingContentView: UIView = {
let view = UIView()

return view
}()

private let photoBackgroundView: UIView = {
let view = UIView()
view.backgroundColor = .white
Expand All @@ -49,6 +73,8 @@ class PhotoDecorateViewController: BaseViewController {
// MARK: - Properties

unowned let viewModel: PhotoDecorateViewModel
var minimumZoomScale = 0.7
var maximumZoomScale = 1.0

// MARK: - Initialize

Expand Down Expand Up @@ -108,68 +134,84 @@ class PhotoDecorateViewController: BaseViewController {
}

private func setupViews() {
view.addSubview(decoratingBorderView)
decoratingBorderView.addSubview(decoratingView)
decoratingView.addSubview(photoBackgroundView)
decoratingView.addSubview(photoView)
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(decoratingAreaView)
decoratingAreaView.addSubview(decoratingView)
decoratingView.addSubview(decoratingContentView)
decoratingContentView.addSubview(photoBackgroundView)
decoratingContentView.addSubview(photoView)

photoView.snp.makeConstraints {
$0.centerY.equalToSuperview()
$0.top.greaterThanOrEqualToSuperview()
$0.bottom.lessThanOrEqualToSuperview()
$0.leading.equalToSuperview().inset(Constraints.photoInset)
$0.trailing.equalToSuperview().inset(Constraints.photoInset)
scrollView.snp.makeConstraints {
$0.edges.equalToSuperview()
}

photoBackgroundView.snp.makeConstraints {
$0.center.equalTo(photoView)
$0.top.edges.equalTo(photoView).inset(-20)
contentView.snp.makeConstraints {
$0.edges.equalTo(scrollView.contentLayoutGuide)
}

decoratingBorderView.snp.makeConstraints {
$0.leading.trailing.equalToSuperview()
$0.top.bottom.equalTo(photoBackgroundView).inset(-20)
decoratingAreaView.snp.makeConstraints {
$0.edges.equalToSuperview()
}

decoratingView.snp.makeConstraints {
$0.edges.equalToSuperview()
}

decoratingContentView.snp.makeConstraints {
$0.edges.equalToSuperview()
$0.center.equalTo(scrollView.frameLayoutGuide)
}

photoBackgroundView.snp.makeConstraints {
$0.leading.trailing.greaterThanOrEqualToSuperview().inset(20)
$0.top.greaterThanOrEqualToSuperview().inset(20)
$0.bottom.lessThanOrEqualToSuperview().inset(20)
$0.center.equalToSuperview()
}

photoView.snp.makeConstraints {
$0.edges.equalTo(photoBackgroundView).inset(Constraints.photoInset)
}
}

// MARK: - Methods

func attachStickerView(_ sticker: ResizableStickerView, at position: CGPoint? = nil) {
photoView.addSubview(sticker)
decoratingContentView.addSubview(sticker)
sticker.center = position ?? photoView.center
addMovingGesture(sticker)
addResizingGesture(sticker)
}

func renderDecoratedImage() -> Data {
let renderer = UIGraphicsImageRenderer(size: decoratingView.bounds.size)
let image = renderer.pngData { context in
decoratingView.drawHierarchy(in: decoratingView.bounds, afterScreenUpdates: true)
}

return image
return decorator.renderDecoratedView(self.decoratingView, in: self.decoratingView.bounds)
}

// MARK: Private

private func addMovingGesture(_ sticker: ResizableStickerView) {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(moveSticker(_:)))

sticker.addGestureRecognizer(panGesture)
}

@objc private func moveSticker(_ gesture: UIPanGestureRecognizer) {
decorator.moveSticker(with: gesture, in: photoView)
decorator.moveSticker(with: gesture, in: self.decoratingContentView)
}

private func addResizingGesture(_ sticker: ResizableStickerView) {
sticker.resizeHandler = { [weak self] gesture, sticker in
guard let self = self else { return }
self.decorator.resizingSticker(sticker, with: gesture, in: self.photoView)
self.decorator.resizingSticker(sticker, with: gesture, in: self.decoratingContentView)
}
}

// MARK: - ScrollViewDelegate
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.decoratingContentView
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
decoratingContentView.center = scrollView.center
}
}
7 changes: 7 additions & 0 deletions 90s/90s/View/PhotoDecorate/Views/ResizableStickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class ResizableStickerView: UIView {
}
}

var isRendering: Bool = false {
willSet {
resizeButton.isHidden = newValue
removeButton.isHidden = newValue
}
}

// MARK: - Intialize

init(image: UIImage?) {
Expand Down
24 changes: 24 additions & 0 deletions 90s/90s/View/PhotoDecorate/Views/StickerDecorator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ class StickerDecorator {
}
}

func renderDecoratedView(_ decoratedView: UIView, in backgroundRect: CGRect) -> Data {
self.setStickers(rendering: true, on: decoratedView)
let renderer = UIGraphicsImageRenderer(size: decoratedView.bounds.size)
let image = renderer.pngData { context in
decoratedView.drawHierarchy(in: backgroundRect, afterScreenUpdates: true)
}
self.setStickers(rendering: false, on: decoratedView)

return image
}

private func setStickers(rendering: Bool, on decoratedView: UIView) {
func recursiveStickerRendering(in view: UIView) {
if let sticker = view as? ResizableStickerView {
sticker.isRendering = rendering
} else {
for subView in view.subviews {
recursiveStickerRendering(in: subView)
}
}
}
recursiveStickerRendering(in: decoratedView)
}

private func resizeStickerAndButtons(sticker: ResizableStickerView, scale: CGFloat, angle: CGFloat) {
let newTransform = CGAffineTransform(scaleX: scale, y: scale).concatenating(CGAffineTransform(rotationAngle: angle))

Expand Down