탭시 SwiftUI에서 직사각형의 오른쪽에 애니메이션을 적용하려고합니다. 하지만 작동하지 않습니다 (비율 상태 var가 있고 애니메이션 가능 유형 (CGFloat)입니다. 이유가 전혀 없습니다. 조언 부탁드립니다.
struct ContentView: View {
@State private var animated = false
var body: some View {
Bar(ratio: animated ? 0.0 : 1.0).animation(Animation.easeIn(duration: 1))
.onTapGesture {
self.animated.toggle()
}.foregroundColor(.green)
}
}
struct Bar: Shape {
@State var ratio: CGFloat
var animatableData: CGFloat {
get { return ratio }
set { ratio = newValue }
}
func path(in rect: CGRect) -> Path {
var p = Path()
p.move(to: CGPoint.zero)
let width = rect.size.width * ratio
p.addLine(to: CGPoint(x: width, y: 0))
let height = rect.size.height
p.addLine(to: CGPoint(x: width, y: height))
p.addLine(to: CGPoint(x: 0, y: height))
p.closeSubpath()
return p
}
}
@State
애니메이션 가능한 데이터 에는 필요하지 않으므로 수정이 간단합니다.
struct Bar: Shape {
var ratio: CGFloat
이 데모보기로 테스트 됨 (Xcode 11.2 / iOS 13.2에서)
struct TestAnimateBar: View {
@State private var animated = false
var body: some View {
VStack {
Bar(ratio: animated ? 0.0 : 1.0).animation(Animation.easeIn(duration: 1))
.foregroundColor(.green)
}
.background(Color.gray)
.frame(height: 40)
.onTapGesture {
self.animated.toggle()
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다