This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 306
/
DevicesView.swift
133 lines (116 loc) · 4.38 KB
/
DevicesView.swift
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// DevicesView.swift
// PagingLayoutSamples
//
// Created by Amir Khorsandi on 30/01/2021.
// Copyright © 2021 Amir Khorsandi. All rights reserved.
//
// Design by Cuberto https://dribbble.com/shots/12580831-Principle-Tutorial-Onboarding-Flow-Animation
import SwiftUI
import CollectionViewPagingLayout
struct Device: Identifiable {
let name: String
let iconName: String
let color: Color
var id: String {
name
}
}
struct DevicesView: View {
private let devices: [Device] = [
Device(name: "iPad", iconName: "ipad", color: .yellow),
Device(name: "Apple Watch", iconName: "applewatch", color: .green),
Device(name: "iPhone", iconName: "iphone", color: .orange),
Device(name: "AirPods Pro", iconName: "airpodspro", color: .blue),
Device(name: "Mac Pro", iconName: "macpro.gen3", color: .red),
Device(name: "HomePod", iconName: "homepod", color: .purple)
]
private let scaleFactor: CGFloat = 130
private let circleSize: CGFloat = 80
@State private var currentDeviceName: String?
var body: some View {
TransformPageView(devices, selection: $currentDeviceName) { device, progress in
ZStack {
roundedRectangle(device: device, progress: progress)
deviceView(device: device, progress: progress)
HStack {
Image(systemName: "chevron.right")
}
.foregroundColor(.white)
.font(.system(size: 30))
.transformEffect(.init(translationX: -300 * (progress - 1), y: 0))
.padding(.top, 400)
.opacity(1 - Double(abs(progress - 1)))
}
}
.animator(DefaultViewAnimator(0.7, curve: .parametric))
.scrollToSelectedPage(false)
.onTapPage { name in
currentDeviceName = currentDeviceName == devices.last?.name ? devices.first?.name : name
}
.zPosition(zPosition)
.collectionView(\.showsHorizontalScrollIndicator, false)
.ignoresSafeArea()
}
private func deviceView(device: Device, progress: CGFloat) -> some View {
VStack {
Image(systemName: device.iconName)
.font(.system(size: 160))
Text(device.name)
.font(.system(size: 40))
.padding(.top, 10)
Spacer()
.frame(maxHeight: 200)
}
.frame(maxHeight: .infinity)
.foregroundColor(.white)
.transformEffect(.init(translationX: 400 * progress, y: 0))
}
private func roundedRectangle(device: Device, progress: CGFloat) -> some View {
let scale = getScale(progress)
return RoundedRectangle(cornerRadius: circleSize * ((0.2 * scaleFactor) / scale))
.fill()
.frame(width: circleSize, height: circleSize)
.scaleEffect(scale, anchor: scaleAnchor(progress))
.transformEffect(.init(translationX: translationX(progress), y: 0))
.padding(.top, 400)
.foregroundColor(device.color)
.opacity((1.25 - max(1, abs(Double(progress)))) / 0.25)
}
private func translationX(_ progress: CGFloat) -> CGFloat {
guard progress >= 1 || progress < -0.5 else { return 0 }
return -2 * (progress + (progress > 0 ? -1 : 1)) * circleSize
}
private func zPosition(_ progress: CGFloat) -> Int {
if progress < -1 { return 3 }
if progress < 0 { return 2 }
if progress < 0.5 { return 1 }
if progress <= 1 { return 4 }
if progress < 1.5 { return 2 }
return -1
}
private func getScale(_ progress: CGFloat) -> CGFloat {
var scale: CGFloat = progress > 1 ? progress - 1 : 1 - progress
if progress <= -1 {
scale = -progress - 1
} else if progress < -0.5 {
scale = progress + 1
} else if progress <= 0.5 {
scale = scaleFactor
}
return 1 + scale * scaleFactor
}
private func scaleAnchor(_ progress: CGFloat) -> UnitPoint {
if progress <= -1 { return .leading }
if progress <= -0.5 { return .trailing }
if progress < 0.5 { return .center }
if progress < 1 { return .leading }
return .trailing
}
}
struct DevicesView_Previews: PreviewProvider {
static var previews: some View {
DevicesView()
.ignoresSafeArea()
}
}