forked from calleric/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeartShape.swift
More file actions
73 lines (61 loc) · 2.49 KB
/
HeartShape.swift
File metadata and controls
73 lines (61 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// HeartView.swift
//
//
import SwiftUI
struct HeartView: View {
var body: some View {
VStack(spacing:50) {
HeartShape()
.fill(Color.red)
.frame(width: 300, height: 300, alignment: .center)
HStack {
HeartShape()
.fill(Color.red)
.frame(width: 150, height: 150, alignment: .center)
HeartShape(cutout: true)
.fill(Color.red, style: FillStyle(eoFill: true))
.frame(width: 150, height: 150, alignment: .center)
}
Spacer()
}
}
}
struct HeartView_Previews: PreviewProvider {
static var previews: some View {
HeartView()
}
}
struct HeartShape: Shape {
var cutout = false
func path(in rect: CGRect) -> Path {
let size = min(rect.width, rect.height) * 0.9
let xOffset = (rect.width > rect.height)
? ((rect.width - rect.height) * 0.5) + size * 0.05
: rect.width * 0.05
let yOffset = (rect.height > rect.width)
? ((rect.height - rect.width) / 2.0) + size * 0.05
: rect.width * 0.05
func offsetPoint(p: CGPoint) -> CGPoint {
return CGPoint(x: p.x + xOffset, y: p.y+yOffset)
}
var path = Path()
if cutout {
path.addPath(Rectangle().path(in: rect))
}
path.move(to: offsetPoint(p: (CGPoint(x: (size * 0.50), y: (size * 0.25)))))
path.addCurve(to: offsetPoint(p: CGPoint(x: 0, y: (size * 0.25))),
control1: offsetPoint(p: CGPoint(x: (size * 0.50), y: (-size * 0.10))),
control2: offsetPoint(p: CGPoint(x: 0, y: 0)))
path.addCurve(to: offsetPoint(p: CGPoint(x: (size * 0.50), y: size)),
control1: offsetPoint(p: CGPoint(x: 0, y: (size * 0.60))),
control2: offsetPoint(p: CGPoint(x: (size * 0.50), y: (size * 0.80))))
path.addCurve(to: offsetPoint(p: CGPoint(x: size, y: (size * 0.25))),
control1: offsetPoint(p: CGPoint(x: (size * 0.50), y: (size * 0.80))),
control2: offsetPoint(p: CGPoint(x: size, y: (size * 0.60))))
path.addCurve(to: offsetPoint(p: CGPoint(x: (size * 0.50), y: (size * 0.25))),
control1: offsetPoint(p: CGPoint(x: size, y: 0)),
control2: offsetPoint(p: CGPoint(x: (size * 0.50), y: (-size * 0.10))))
return path
}
}