0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「うるさい猫をだまらせろ」機能追加

Posted at

以前作成した夜中に騒ぐ飼い猫たちを宥めるサウンドアプリを改良しました。
改良点は

  • タップすると少し沈むアニメーション付きのボタン
  • マナーモードでもちゃんと鳴るサウンド
  • 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)")
    }

    // ...以降の音声プレイヤー初期化処理...
}

今後は猫たちの体調管理ツールとしても進化させていきたいですね

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?