@@ -2,61 +2,78 @@ import SwiftUI
2
2
import UIKit
3
3
4
4
struct GradientView : View {
5
+
6
+ @State private var stage : Double = 0
7
+ private let maxStages : Double = 10.0
8
+
9
+ private let startColor : Color = . purple
10
+ private let endColor : Color = . blue. darker ( by: 2 )
11
+
12
+ private let timer = Timer . publish ( every: 0.1 , on: . main, in: . commonModes) . autoconnect ( ) // adjust as needed
13
+
5
14
var body : some View {
6
15
ZStack {
7
- RadialGradient (
8
- gradient: Gradient ( colors: [ Color ( hue: 198 / 360 , saturation: 1 , brightness: 0.8 ) , Color . clear] ) ,
9
- center: . init( x: 0.82 , y: 0.65 ) ,
10
- startRadius: 0 ,
11
- endRadius: 0.55 * UIScreen. main. bounds. width
12
- )
13
- RadialGradient (
14
- gradient: Gradient ( colors: [ Color ( hue: 220 / 360 , saturation: 0.37 , brightness: 0.97 ) , Color . clear] ) ,
15
- center: . init( x: 0.47 , y: 0.33 ) ,
16
- startRadius: 0 ,
17
- endRadius: 0.59 * UIScreen. main. bounds. width
18
- )
19
-
20
- } . background (
21
- LinearGradient ( gradient: Gradient ( colors: [ . purple, . blue] ) , startPoint: . top, endPoint: . bottom) . edgesIgnoringSafeArea ( /*@START_MENU_TOKEN@*/. all/*@END_MENU_TOKEN@*/)
22
- )
16
+ ForEach ( 0 ..< ( Int ( maxStages) ) , id: \. self) { i in
17
+ gradient ( for: i)
18
+ . opacity ( opacity ( for: i) )
19
+ . animation ( . linear( duration: 0.4 ) , value: self . stage)
20
+
21
+ }
22
+ }
23
+ . edgesIgnoringSafeArea ( . all)
24
+ . onAppear {
25
+ Timer . scheduledTimer ( withTimeInterval: 0.5 , repeats: true ) { _ in
26
+ withAnimation {
27
+ self . stage = ( self . stage + 1 )
28
+ }
29
+ }
30
+ }
23
31
}
24
32
25
- }
26
-
27
- struct GradientBackgroundRepresentable : UIViewControllerRepresentable {
33
+ private func gradient( for index: Int ) -> LinearGradient {
34
+ print ( index)
35
+ let points = gradientPoints ( for: index)
36
+ return LinearGradient (
37
+ gradient: Gradient ( colors: [ startColor, endColor] ) ,
38
+ startPoint: points. 0 ,
39
+ endPoint: points. 1
40
+ )
41
+ }
28
42
29
- func makeUIViewController( context: Context ) -> UIViewController {
30
- let hostingController = UIHostingController ( rootView: GradientView ( ) )
31
- return hostingController
43
+ private func gradientPoints( for index: Int ) -> ( UnitPoint , UnitPoint ) {
44
+ switch index {
45
+ case 0 :
46
+ return ( . topLeading, . bottomTrailing)
47
+ case 1 :
48
+ return ( . top, . bottom)
49
+ case 2 :
50
+ return ( . topTrailing, . bottomLeading)
51
+ case 3 :
52
+ return ( . trailing, . leading)
53
+ case 4 :
54
+ return ( . bottomTrailing, . topLeading)
55
+ case 5 :
56
+ return ( . bottom, . top)
57
+ case 6 :
58
+ return ( . bottomLeading, . topTrailing)
59
+ case 7 :
60
+ return ( . leading, . trailing)
61
+ case 8 :
62
+ return ( . topLeading, . bottomTrailing)
63
+ default :
64
+ return ( . topLeading, . bottomTrailing)
65
+ }
32
66
}
33
67
34
- func updateUIViewController( _ uiViewController: UIViewController , context: Context ) {
35
-
68
+ private func opacity( for index: Int ) -> Double {
69
+ let adjustedStage = ( self . stage - Double( index) ) / self . maxStages
70
+ return max ( 0 , 0.5 * cos( 2 * Double. pi * adjustedStage) + 0.5 )
36
71
}
37
72
}
38
- class GradientBackgroundViewController : UIViewController {
39
-
40
- override func viewDidLoad( ) {
41
- super. viewDidLoad ( )
42
-
43
- let gradientBackgroundView = UIHostingController ( rootView: GradientView ( ) )
44
- // Add as a child of the current view controller.
45
- addChildViewController ( gradientBackgroundView)
46
73
47
- // Add the SwiftUI view to the view controller view hierarchy.
48
- view. addSubview ( gradientBackgroundView. view)
49
74
50
- // Setup constraints to update the SwiftUI view boundaries.
51
- // gradientBackgroundView.view.translatesAutoresizingMaskIntoConstraints = false
52
- // NSLayoutConstraint.activate([
53
- // gradientBackgroundView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
54
- // gradientBackgroundView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
55
- // gradientBackgroundView.view.topAnchor.constraint(equalTo: view.topAnchor),
56
- // gradientBackgroundView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
57
- // ])
58
- //
59
- // // Notify the hosting controller that it has been moved to the current view controller.
60
- // gradientBackgroundView.didMove(toParent: self)
75
+ struct GradientView_Previews : PreviewProvider {
76
+ static var previews : some View {
77
+ GradientView ( )
61
78
}
62
79
}
0 commit comments