Created
April 9, 2023 10:15
-
-
Save iAmVishal16/100824aa0672204a6eb6a54a4bc8d878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OptimisedTriangleAnim.swift | |
// ShapesFun | |
// | |
// Created by Vishal Paliwal on 22/01/23. | |
// | |
import SwiftUI | |
struct OptimisedTriangleAnim: View { | |
@State var isAnimating = false | |
@State var scale = 1 | |
var body: some View { | |
ZStack { | |
Color.black | |
.ignoresSafeArea() | |
ZStack { | |
ForEach(0..<8) { index in | |
let triangle = Triangle() | |
triangle.stroke(lineWidth: 1 + Double(index) * 0.1) | |
.fill(LinearGradient(colors: color(at: index), startPoint: .leading, endPoint: .trailing)) | |
.frame(width: 40 + Double(index) * 60, height: 40 + Double(index) * 60, alignment: .center) | |
.scaleEffect(isAnimating ? CGFloat(scale) : 0) | |
.offset(x: isAnimating ? 100 : -100) | |
.opacity(isAnimating ? 1 - Double(index) * 0.1 : 0.2 + Double(index) * 0.1) | |
.animation(Animation.easeOut(duration: 2.5).repeatForever(autoreverses: true).delay(Double(index) * 0.1), value: isAnimating) | |
// .animation(Animation.interpolatingSpring(stiffness: 15, damping: 3).repeatForever(autoreverses: true).delay(Double(index) * 0.1), value: isAnimating) | |
} | |
} | |
} | |
.onAppear { | |
isAnimating.toggle() | |
scale = 2 | |
} | |
} | |
func color(at index: Int) -> [Color] { | |
switch index { | |
case 0: | |
return [.red, .green, .blue] | |
case 1: | |
return [.blue, .green, .yellow] | |
case 2: | |
return [.yellow, .pink, .blue] | |
case 3: | |
return [.blue, .purple, .green] | |
case 4: | |
return [.green, .blue, .cyan] | |
case 5: | |
return [.cyan, .orange, .teal] | |
case 6: | |
return [.teal, .mint, .white] | |
case 7: | |
return [.black, .teal, .blue] | |
default: | |
return [.mint, .black, .brown] | |
} | |
return [.blue, .purple, .green] | |
} | |
} | |
struct OptimisedTriangleAnim_Previews: PreviewProvider { | |
static var previews: some View { | |
OptimisedTriangleAnim() | |
} | |
} | |
struct Triangle: Shape { | |
func path(in rect: CGRect) -> Path { | |
var path = Path() | |
path.move(to: CGPoint(x: rect.midX, y: rect.minY)) | |
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY)) | |
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) | |
path.addLine(to: CGPoint(x: rect.midX, y: rect.minY)) | |
return path | |
} | |
} | |
struct Triangle_Previews: PreviewProvider { | |
static var previews: some View { | |
Triangle() | |
.preferredColorScheme(.dark) | |
.frame(width: 200, height: 200, alignment: .center) | |
.previewLayout(.sizeThatFits) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment