以前作成した夜中に騒ぐ飼い猫たちを宥めるサウンドアプリを改良しました。
改良点は
- タップすると少し沈むアニメーション付きのボタン
- マナーモードでもちゃんと鳴るサウンド
- 1回のタップで2回繰り返される音声
- たまに現れる隠しキャラ
ButtonStyleの作成
まず、ボタンの見た目と動作を定義するカスタムスタイルを作成します。
// ボタンが押された時のアニメーションを定義
struct BouncyButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
// configuration.isPressedが true (押されている) なら少し小さくする
.scaleEffect(configuration.isPressed ? 0.95 : 1.0)
// アニメーションを滑らかにする
.animation(.easeInOut(duration: 0.1), value: configuration.isPressed)
}
}
###マナーモードでも音がなるようにできるレシピを学びました。
import AVFoundation
// ...
init() {
// アプリ全体の音の振る舞いを設定
do {
// カテゴリを.playbackに設定することで、サイレントスイッチを無視する
try AVAudioSession.sharedInstance().setCategory(.playback)
// 設定を有効化する
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("Failed to set audio session category: \(error)")
}
// ...以降の音声プレイヤー初期化処理...
}
今後は猫たちの体調管理ツールとしても進化させていきたいですね