animation
모디파이어 사용struct AnimationBasics: View {
@State private var position = CGPoint.zero
var body: some View {
VStack {
Circle()
.foregroundColor(.blue)
.frame(width: 50, height: 50)
.position(position)
.offset(x: 50, y: 50)
.animation(.default, value: position) //value값의 변화에 따라
Spacer()
Button("Animate") {
position = position == .zero ? CGPoint(x: 300, y: 500) : .zero
}
.padding()
}
}
}
easeInOut
많이 사용easeInOut(duration:)
사용.animation(.easeInOut(duration: 3), value: position)
.delay
: 애니메이션 시작 시점 지연.animation(.easeInOut(duration: 3).speed(2), value: position)
withAnimation
메소드 사용
Button("Animate") {
withAnimation(.linear){
position = position == .zero ? CGPoint(x: 300, y: 500) : .zero
}
}
repeatCount
모디파이어 사용 시 유한적 횟수만큼 애니메이션 반복repeatForever
모디파이어 사용 시 무한히 애니메이션 반복@State private var animating = false
var finiteRepeat: Animation {
.linear(duration: 1)
.repeatCount(3, autoreverses: false)//autoreverse 기본값 true
}
var infiniteRepeat: Animation {
.linear(duration: 1)
.repeatForever(autoreverses: false)
}
var body: some View {
VStack {
Image(systemName: "arrow.2.circlepath")
.resizable()
.foregroundColor(.blue)
.aspectRatio(contentMode: .fit)
.frame(width: 200, height: 200)
.rotationEffect(.degrees(animating ? 360 : 0))
.animation(finiteRepeat, value: animating)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.onAppear {
animating = true
}
}
autoreverse: true
의 경우(기본값)
autoreverse: false
의 경우
Circle()
.foregroundColor(.blue)
.frame(width: 50, height: 50)
.position(position)
.offset(x: 50, y: 50)
.animation(.spring(response: 0.3, dampingFraction: 0.3, blendDuration: 0))
spring
모디파이어 사용response
값이 작을수록 짧은 시간내에 dampingdamping
값이 작을수록 멈추지 않고, 클수록 스프링 효과 없이 최종 위치에서 바로 끝남(0~1)blendDuration
: 두 개 이상의 스프링 애니메이션을 동시에 추가했을 때 사용(기본값 0)