-
Notifications
You must be signed in to change notification settings - Fork 43
/
PositionSpringAnimation.swift
56 lines (44 loc) · 1.45 KB
/
PositionSpringAnimation.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
//
// PositionSpringAnimation.swift
// Learning SwiftUI Spring Animations: The Basics and Beyond
//
// Created by Amos from getstream.io
//
import SwiftUI
struct PositionSpringAnimation: View {
// 1. Define an animation variable
@State private var moving = false
var body: some View {
VStack {
Text("Animate a ball's position so that it appears to be pulled towards a target by a spring.")
.font(.title)
.multilineTextAlignment(.center)
Spacer()
Image("ball")
.offset(x: moving ? 150 : -150)
// 2. Add animation trigger
.onAppear {
withAnimation(
.spring()
//.interactiveSpring()
.repeatForever(
autoreverses: false)){
// Set final animation state
moving.toggle()
}
}
HStack(spacing: 300) {
Text("A")
Text("B")
}
Spacer()
}
.padding()
}
}
struct PositionSpringAnimation_Previews: PreviewProvider {
static var previews: some View {
PositionSpringAnimation()
.preferredColorScheme(.dark)
}
}