摇一摇

#import <AudioToolbox/AudioToolbox.h>

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    // iOS,音频包括短效音频和音乐, 短效音频的实现用的是系统的AudioToolBox, 音乐播放使用的是AVFoundation框架

    // 短效音频: 播放过程不可控, 长度不超过30

    

    // 定义一个系统声音id

    SystemSoundID soundId;

    

    // 设置播放短效音频的资源

    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([[NSBundle mainBundle] URLForResource:@"shake" withExtension:@"wav"]), &soundId);

    

    // 设置震动的效果, 需要真机

//    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    

     // 播放短效音频

    AudioServicesPlaySystemSound(soundId);

 

 

//很简单,你只需要让这个Controller本身支持摇动
 
//同时让他成为第一相应者:
    [[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];
 
    [selfbecomeFirstResponder];

    

}

 

// 摇一摇 依赖于重力感应原理

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    

    // 定义一个系统声音id

    SystemSoundID soundId;

    

    // 设置播放短效音频的资源

    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([[NSBundle mainBundle] URLForResource:@"shake" withExtension:@"wav"]), &soundId);

    

    // 设置震动的效果, 需要真机

    //    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

    

    // 播放短效音频

    AudioServicesPlaySystemSound(soundId);

    

    NSLog(@"摇动开始");

}

 

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    

    NSLog(@"摇动结束");

}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {

    

    NSLog(@"摇动取消");

}

转载于:https://www.cnblogs.com/daxueshan/p/6183838.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值