Skip to content

Instantly share code, notes, and snippets.

@Koshimizu-Takehito
Last active September 17, 2023 05:16
Show Gist options
  • Save Koshimizu-Takehito/8196f0a6860ab789a15dd5b7efc05449 to your computer and use it in GitHub Desktop.
Save Koshimizu-Takehito/8196f0a6860ab789a15dd5b7efc05449 to your computer and use it in GitHub Desktop.
ホームスクリーンのアイコンのブルブルするやつ
import SwiftUI
let symbols = [
"pencil",
"trash",
"folder",
"camera",
"photo",
"clock",
"arrow.right.circle.fill",
"plus",
"multiply",
"square.and.arrow.up",
"bubble.left",
"heart",
"person",
"bolt",
"star",
"exclamationmark.triangle"
]
let colors: [Color] = [
.black,
.gray,
.red,
.green,
.blue,
.orange,
.cyan,
.brown,
.mint,
.yellow,
.pink,
.indigo,
.purple
]
struct ContentView: View {
@State var parameter: Double = 0
let columns = [GridItem](repeating: GridItem(), count: 4)
var body: some View {
LazyVGrid(columns: columns, spacing: 16) {
ForEach(0..<15) { i in
SampleIcon(symbol: symbols[i], color: colors[i % colors.count])
.frame(width: 80, height: 80)
.modifier(OscillatingEffect(parameter))
}
}
.padding()
.onAppear {
withAnimation(.linear(duration: 0.3).repeatForever()) {
parameter = 1
}
}
}
}
struct SampleIcon: View {
var symbol: String
var color: Color
var body: some View {
ZStack {
RoundedRectangle(cornerSize: .init(width: 10, height: 10))
.foregroundStyle(color)
Image(systemName: symbol)
.font(.largeTitle)
.foregroundStyle(.white)
}
}
}
struct OscillatingEffect: ViewModifier, Animatable {
var animatableData: Double // 0...1
let angle: Angle = .degrees(2)
let offset: CGSize = .init(width: 0.5, height: 0.25)
let random: Double = .random(in: -1..<1)
init(_ parameter: Double) {
self.animatableData = parameter
}
func body(content: Content) -> some View {
content
.rotationEffect(animatableAngle())
.offset(animatableOffset())
}
func animatableAngle() -> Angle {
.radians((cos(animatableData * 2 * .pi) + random) * angle.radians)
}
func animatableOffset() -> CGSize {
let x = cos(animatableData * 2 * .pi + random * .pi) * offset.width
let y = sin(animatableData * 4 * .pi + random * .pi) * offset.height
return CGSize(width: x, height: y)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment