Button()はタップすると明暗変化?している。
ボタン画像をタップした時に拡大縮小してアピールを大きくしたい。
新しい iOS に制限されるけど、androidOSより簡単にアニメーションです。
【MyApp.swift】
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
【ContentView.swift】
struct ContentView: View {
var body: some View {
ButtonView()
}
}
・ボタンパーツ
ボタンに使う画像をPNGファイルで用意して背景を透明設定しておくと良い。
【ButtonView.swift】
struct ButtonView: View {
@State var scale: CGFloat = 2.0 // 大きさ調整
var body: some View {
Image(systemName:"arrowshape.forward.circle.fill")
.gesture(
TapGesture()
.onEnded{_ in
ZoomAnimation()
/* 処理本体を書く */
}
)
.scaleEffect(scale)
}
func ZoomAnimation() { // ボタン代わりの画像を拡大縮小させる
withAnimation(Animation.linear(duration: 0.1)){ // 変化する時間も設定
scale = 3.0
} completion: { // ios17以降?
withAnimation(Animation.linear(duration: 0.1)){
scale = 2.0
}
}
}
}