重力感应

重力感应一般是用来做摇一摇的

import UIKit
import AVFoundation

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    //重力感应:摇一摇
    override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
        print("摇动开始")
        
        //摇一摇过程中一般会伴随震动或者声音
        //短效音频实现,30秒以下
        //声明一个系统声音ID 必须初始化
        var soundID: SystemSoundID = 1
        //设置播放源
        //参数1:本地短效音频资源路径
        //参数2:系统声音ID地址
        let path = NSBundle.mainBundle().pathForResource("shake", ofType: "wav")
        AudioServicesCreateSystemSoundID(NSURL(fileURLWithPath: path!), &soundID)
        
        //设置震动效果
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        
        //设置声音提示
        AudioServicesPlaySystemSound(soundID)
        
    }
    
    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
        print("摇动结束")
    }
    
    override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?) {
        print("取消摇动")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

你可能感兴趣的:(重力感应)