Skip to content

Commit 2d55c9a

Browse files
committed
feat: 서로 다른 모서리에 각기 다른 CornerRadius 값을 줄 수 있도록 코드 구현
1 parent a0bd79c commit 2d55c9a

6 files changed

Lines changed: 172 additions & 10 deletions

File tree

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/TSAlert/TSAlertController+ViewConfiguration.swift

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public extension TSAlertController {
7878
public var shadow: Shadow?
7979

8080
/// The corner radius of the alert view.
81-
public var cornerRadius: CGFloat
81+
public var cornerRadius: CornerRadius
8282

8383
/// The background color of the dimmed overlay behind the alert.
8484
public var dimmedBackgroundViewColor: Background?
@@ -95,6 +95,46 @@ public extension TSAlertController {
9595
/// The axis layout of the button group (horizontal or vertical).
9696
public var buttonGroupAxis: ButtonGroupAxis
9797

98+
///
99+
public struct CornerRadius: ExpressibleByFloatLiteral {
100+
101+
///
102+
public var topLeft: CGFloat
103+
104+
///
105+
public var topRight: CGFloat
106+
107+
///
108+
public var bottomLeft: CGFloat
109+
110+
///
111+
public var bottomRight: CGFloat
112+
113+
///
114+
public init(allCorners: CGFloat = 20) {
115+
self.init(topLeft: allCorners,
116+
topRight: allCorners,
117+
bottomLeft: allCorners,
118+
bottomRight: allCorners)
119+
}
120+
121+
///
122+
public init(topLeft: CGFloat = 20,
123+
topRight: CGFloat = 20,
124+
bottomLeft: CGFloat = 20,
125+
bottomRight: CGFloat = 20) {
126+
self.topLeft = topLeft
127+
self.topRight = topRight
128+
self.bottomLeft = bottomLeft
129+
self.bottomRight = bottomRight
130+
}
131+
132+
///
133+
public init(floatLiteral value: FloatLiteralType) {
134+
self.init(allCorners: value)
135+
}
136+
}
137+
98138
/// Defines the shadow properties of the alert.
99139
public struct Shadow {
100140

@@ -172,7 +212,7 @@ public extension TSAlertController {
172212
backgroundBorderColor: CGColor? = nil,
173213
backgroundBorderWidth: CGFloat = 0,
174214
shadow: Shadow? = nil,
175-
cornerRadius: CGFloat = 20,
215+
cornerRadius: CornerRadius = .init(allCorners: 20),
176216
dimmedBackgroundViewColor: Background? = .color(.black.withAlphaComponent(0.75)),
177217
margin: LayoutMargin = .init(),
178218
spacing: LayoutSpacing = .init(),

Sources/TSAlert/TSAlertController.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ public final class TSAlertController: UIViewController {
233233
activateFirstResponderIfNeeded()
234234
}
235235

236+
public override func viewDidLayoutSubviews() {
237+
super.viewDidLayoutSubviews()
238+
239+
view.applyRoundCorners(viewConfiguration.cornerRadius)
240+
}
241+
236242
public override func viewDidDisappear(_ animated: Bool) {
237243
super.viewDidDisappear(animated)
238244

@@ -303,6 +309,7 @@ public final class TSAlertController: UIViewController {
303309
/// based on `TSAlertController.Style`.
304310
/// This ensures that the alert is presented correctly and prevents unintended behavior.
305311
private func adjustViewConfigurationForcibly() {
312+
adjustCornerRadiusForcibly()
306313
adjustButtonGroupAxisForcibly()
307314
adjustActionOrderForcibly()
308315
}
@@ -333,6 +340,17 @@ public final class TSAlertController: UIViewController {
333340
}
334341
}
335342

343+
///
344+
private func adjustCornerRadiusForcibly() {
345+
switch preferredStyle {
346+
case .actionSheet:
347+
viewConfiguration.cornerRadius.bottomLeft = 0
348+
viewConfiguration.cornerRadius.bottomRight = 0
349+
default:
350+
break
351+
}
352+
}
353+
336354
private func initializeAlertView() {
337355
setupAlertView()
338356
setupConstraints()
@@ -384,7 +402,7 @@ public final class TSAlertController: UIViewController {
384402

385403
view.layer.borderColor = viewConfiguration.backgroundBorderColor
386404
view.layer.borderWidth = viewConfiguration.backgroundBorderWidth
387-
view.layer.cornerRadius = viewConfiguration.cornerRadius
405+
view.applyRoundCorners(viewConfiguration.cornerRadius)
388406

389407
guard let shadow = viewConfiguration.shadow else { return }
390408
view.layer.shadowColor = shadow.color

Sources/TSAlert/TSAlertView/DefaultAlertView/DefaultAlertView.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,11 @@ class DefaultAlertView: UIView, TSAlertView {
8787
let actionHeight: CGFloat = viewConfiguration.buttonHeight
8888
let spacing: CGFloat = viewConfiguration.spacing.buttonSpacing
8989

90-
let height: CGFloat = if !isEmpty {
91-
isHorizontal
90+
var height: CGFloat = 0
91+
if !isEmpty {
92+
height = isHorizontal
9293
? actionHeight
9394
: (actionHeight * actionsCount) + ((actionsCount - 1) * spacing)
94-
} else {
95-
0
9695
}
9796
buttonGroupView.setHeight(equalTo: height)
9897
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// UIBezierPath+Extension.swift
3+
// TSAlertController
4+
//
5+
// Created by 김건우 on 3/21/25.
6+
//
7+
8+
import UIKit
9+
10+
extension UIBezierPath {
11+
12+
convenience init(roundedRect rect: CGRect,
13+
topLeftRadius: CGFloat = .zero,
14+
topRightRadius: CGFloat = .zero,
15+
bottomLeftRadius: CGFloat = .zero,
16+
bottomRightRadius: CGFloat = .zero) {
17+
self.init()
18+
19+
let path = CGMutablePath()
20+
21+
let topLeftPoint = CGPoint(x: rect.minX, y: rect.minY)
22+
let topRightPoint = CGPoint(x: rect.maxX, y: rect.minY)
23+
let bottomLeftPoint = CGPoint(x: rect.minX, y: rect.maxY)
24+
let bottomRightPoint = CGPoint(x: rect.maxX, y: rect.maxY)
25+
26+
path.move(to: bottomLeftPoint)
27+
28+
path.addArc(tangent1End: topLeftPoint,
29+
tangent2End: topRightPoint,
30+
radius: topLeftRadius)
31+
32+
path.addArc(tangent1End: topRightPoint,
33+
tangent2End: bottomRightPoint,
34+
radius: topRightRadius)
35+
36+
path.addArc(tangent1End: bottomRightPoint,
37+
tangent2End: bottomLeftPoint,
38+
radius: bottomRightRadius)
39+
40+
path.addArc(tangent1End: bottomLeftPoint,
41+
tangent2End: topLeftPoint,
42+
radius: bottomLeftRadius)
43+
44+
path.closeSubpath()
45+
cgPath = path
46+
}
47+
}
48+
49+
50+
extension CGMutablePath {
51+
52+
func addLine(toX x: CGFloat, y: CGFloat) {
53+
addLine(to: CGPoint(x: x, y: y))
54+
}
55+
56+
func move(toX x: CGFloat, y: CGFloat) {
57+
move(to: CGPoint(x: x, y: y))
58+
}
59+
}

Sources/Utils/Extensions/UIView+Extension.swift

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ extension UIView {
219219
if let configuration = configuration {
220220
blurEffectView.layer.borderColor = configuration.backgroundBorderColor
221221
blurEffectView.layer.borderWidth = configuration.backgroundBorderWidth
222-
blurEffectView.layer.cornerRadius = configuration.cornerRadius
222+
// blurEffectView.layer.cornerRadius = configuration.cornerRadius
223223
blurEffectView.layer.masksToBounds = true
224224
}
225225
insertSubview(blurEffectView, at: 0)
@@ -242,14 +242,14 @@ extension UIView {
242242
locations)
243243

244244
if let viewConfiguration = viewConfiguration {
245-
gradientView.gradientLayer?.cornerRadius = viewConfiguration.cornerRadius
245+
// gradientView.gradientLayer?.cornerRadius = viewConfiguration.cornerRadius
246246
gradientView.gradientLayer?.masksToBounds = true
247247
}
248248
insertSubview(gradientView, at: 0)
249249
gradientView.fill(to: self)
250250
}
251251

252-
class GradientView: UIView {
252+
final class GradientView: UIView {
253253

254254
let gradientLayer: CAGradientLayer?
255255

@@ -310,3 +310,45 @@ extension UIView {
310310
self.layer.anchorPoint = anchorPoint
311311
}
312312
}
313+
314+
extension UIView {
315+
316+
// MARK: - Apply Round Corners
317+
318+
///
319+
func applyRoundCorners(_ cornerRadius: CGFloat) {
320+
321+
applyRoundCorners(topLeftRadius: cornerRadius,
322+
topRightRadius: cornerRadius,
323+
bottomLeftRadius: cornerRadius,
324+
bottomRightRadius: cornerRadius)
325+
}
326+
327+
///
328+
func applyRoundCorners(_ cornerRadius: TSAlertController.ViewConfiguration.CornerRadius) {
329+
330+
applyRoundCorners(topLeftRadius: cornerRadius.topLeft,
331+
topRightRadius: cornerRadius.topRight,
332+
bottomLeftRadius: cornerRadius.bottomLeft,
333+
bottomRightRadius: cornerRadius.bottomRight)
334+
}
335+
336+
///
337+
func applyRoundCorners(topLeftRadius: CGFloat,
338+
topRightRadius: CGFloat,
339+
bottomLeftRadius: CGFloat,
340+
bottomRightRadius: CGFloat) {
341+
342+
let roundedRect = self.bounds
343+
344+
let path = UIBezierPath(roundedRect: roundedRect,
345+
topLeftRadius: topLeftRadius,
346+
topRightRadius: topRightRadius,
347+
bottomLeftRadius: bottomLeftRadius,
348+
bottomRightRadius: bottomRightRadius)
349+
350+
let shapeLayer = CAShapeLayer()
351+
shapeLayer.path = path.cgPath
352+
layer.mask = shapeLayer
353+
}
354+
}

0 commit comments

Comments
 (0)