-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathOvalShapeView.swift
57 lines (49 loc) · 2.16 KB
/
OvalShapeView.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
//
// ContentView.swift
// OvalShape
//
// Created by Ganesh on 18/05/23.
//
import SwiftUI
struct OvalShapeView: View {
var body: some View {
OvalShape()
}
}
struct OvalShape: View {
// MARK: Properties
@State private var appear = false
//MARK: - Body
var body: some View {
TimelineView(.animation) { timeline in
let now = timeline.date.timeIntervalSinceReferenceDate
let angle = Angle.degrees(now.remainder(dividingBy: 3) * 60)
let x = cos(angle.radians)
let angle2 = Angle.degrees(now.remainder(dividingBy: 6) * 10)
let x2 = cos(angle2.radians)
Canvas{ context,size in
context.fill(path(in: CGRect(x: 0, y: 0, width: size.width, height: size.height), x: x, x2: x2), with: .linearGradient(Gradient(colors: [Color("red"),Color("orange")]), startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 400, y: 400)))
}
.frame(width: 400,height: 400)
.rotationEffect(.degrees(appear ? 360:30))
}
.onAppear{
withAnimation(.linear(duration:10).repeatForever(autoreverses: true)){
appear = true
}
}
}
// MARK: Custom Shape
func path(in rect: CGRect,x:Double,x2:Double) -> Path {
var path = Path()
let width = rect.size.width
let height = rect.size.height
path.move(to: CGPoint(x: width, y: 0.66398*height))
path.addCurve(to: CGPoint(x: 0.5*width*x2, y: height), control1: CGPoint(x: width, y: 0.84956*height), control2: CGPoint(x: 0.77614*width*x2, y: height))
path.addCurve(to: CGPoint(x: 0, y: 0.66398*height), control1: CGPoint(x: 0.22386*width*x2, y: height), control2: CGPoint(x: 0, y: 0.84956*height))
path.addCurve(to: CGPoint(x: 0.5*width, y: 0), control1: CGPoint(x: 0, y: 0.4784*height*x2), control2: CGPoint(x: 0.22386*width, y: 0))
path.addCurve(to: CGPoint(x: width, y: 0.66398*height), control1: CGPoint(x: 0.77614*width*x2, y: 0), control2: CGPoint(x: width, y: 0.4784*height))
path.closeSubpath()
return path
}
}