LoginSignup
10
7

More than 3 years have passed since last update.

【Swift】バイブレーションを使う

Posted at

はじめに

今回はバイブレーション機能を使ってみます。

GitHub

実装

スクリーンショット 2021-04-23 17.39.44.png

import UIKit
import AudioToolbox

final class VibrationViewController: UIViewController {

    @IBAction private func longStrongOneButtonDidTapped(_ sender: Any) {
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
    }

    @IBAction private func shortStrongOneButtonDidTapped(_ sender: Any) {
        AudioServicesPlaySystemSound(1520)
    }

    @IBAction private func shortStrongTwoButtonDidTapped(_ sender: Any) {
        AudioServicesPlaySystemSound(1011)
    }

    @IBAction private func shortWeakOneButtonDidTapped(_ sender: Any) {
        AudioServicesPlaySystemSound(1519)
    }

    @IBAction private func shortWeakTwoButtonDidTapped(_ sender: Any) {
        AudioServicesPlaySystemSound(1102)
    }

    @IBAction private func shortWeakThreeButtonDidTapped(_ sender: Any) {
        AudioServicesPlaySystemSound(1521)
    }

}

解説

バイブレーション機能を使うために、AudioToolboximportします。

import AudioToolbox

1回長くて強いバイブレーション

@IBAction private func longStrongOneButtonDidTapped(_ sender: Any) {
    AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}

1回短くて強いバイブレーション

@IBAction private func shortStrongOneButtonDidTapped(_ sender: Any) {
    AudioServicesPlaySystemSound(1520)
}

2回短くて強いバイブレーション

@IBAction private func shortStrongTwoButtonDidTapped(_ sender: Any) {
    AudioServicesPlaySystemSound(1011)
}

1回短くて弱いバイブレーション

@IBAction private func shortWeakOneButtonDidTapped(_ sender: Any) {
    AudioServicesPlaySystemSound(1519)
}

2回短くて弱いバイブレーション

@IBAction private func shortWeakTwoButtonDidTapped(_ sender: Any) {
    AudioServicesPlaySystemSound(1102)
}

3回短くて弱いバイブレーション

@IBAction private func shortWeakThreeButtonDidTapped(_ sender: Any) {
    AudioServicesPlaySystemSound(1521)
}

おわりに

あるのとないので使い心地が変わりそうですね。

10
7
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
10
7