どうも、ちょげ(@chogetarou)です。
ビューを回転するアニメーションをする方法を紹介します。
方法

回転アニメーションをするには、rotationEffect修飾子を使います。
まず、ビューにrotationEffect修飾子を付与します。
rotationEffect修飾子の引数には、Angleでビューの回転角度(0〜360)を指定します。
次に、ビューにアニメーションを付与します。
ExampleView(・・・)
.rotationEffect(degree)
.animation(・・・)
最後に、rotationEffect内に指定したビューの回転角度を変更します。
使用例
struct ContentView: View {
@State var isRotate = false
var body: some View {
VStack {
Rectangle()
.frame(width: 200, height: 200)
.foregroundColor(.pink)
.rotationEffect(Angle.degrees(isRotate ? 0 : 360))
.animation(.easeInOut(duration: 2), value: isRotate)
Button(action: {
self.isRotate.toggle()
}) {
Text("Rotate")
}
}
}
}
オススメの記事
コメント