zoukankan      html  css  js  c++  java
  • 摇一摇

    #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(@"摇动取消");

    }

  • 相关阅读:
    CSS3实现0.5px的边框
    解决nvm导致终端启动慢的问题
    解决zsh: command not found: gitk,将git路径/usr/bin/git修改为/usr/local/bin/git
    MAC之zsh终端使用nvm安装指定版本node
    解决npm包node-sass下载失败问题
    forEach中使用async await的问题
    使用module-alias别名简化CommonJS的require路径
    关系型数据库的ACID
    Node.js ORM 框架对比
    mysql字符集 utf8 和utf8mb4 的区别
  • 原文地址:https://www.cnblogs.com/daxueshan/p/6183838.html
Copyright © 2011-2022 走看看