iOS摇一摇
2015-11-29 00:00
281 查看
摘要: iOS摇一摇
一、直接用系统自带的motionBegan方法 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 假如程序不响应此方法,试着加入下面方法: -(BOOL)canBecomeFirstResponder { return YES; } 如果还不行,建议用第二种方法。 二、motionBegan+通知的方法 1.在Appdelegate里写motionBegan方法 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter]postNotificationName:@"shake" object:self]; } 2.在需要接收通知的页面添加通知[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shakeAction) name:@"shake" object:nil]; 写在viewDidLoad里即可。这里的shakeAction就是摇一摇需要调用的方法,自己修改,通知名字对应就好,可自由修改。 #import <AudioToolbox/AudioToolbox.h> #import <AVFoundation/AVFoundation.h> #import "ShakeViewController.h" @interface ShakeViewController () @property (nonatomic, strong)AVAudioPlayer *player; @end @implementation ShakeViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.titleLabel.text = @"摇一摇"; } - (BOOL)becomeFirstResponder{ return YES; } - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ [self shake]; [self sound]; } - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ } - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ } - (void)shake{ AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate); } - (void)sound{ /* SystemSoundID systemID; NSString *path = [[NSBundle mainBundle] pathForResource:@"shake_sound_male.wav" ofType:@""]; NSURL *url = [NSURL URLWithString:path]; //NSURL *url = [NSURL fileURLWithPath:path]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &systemID); AudioServicesPlaySystemSound(systemID); */ NSString *path = [[NSBundle mainBundle] pathForResource:@"shake_sound_male.wav" ofType:@""]; NSURL *url = [NSURL fileURLWithPath:path]; _player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; [_player prepareToPlay]; [_player play]; }
相关文章推荐
- iOS开发>学无止境 - 加速Table Views开发的10个建议
- iOS进阶之runtime(1)开篇--初入江湖
- iOS开发>学无止境 - 关于iOS多线程,你看我就够了
- iOS开发>学无止境 - 保存照片到系统相册(Photo Album)
- iOS开发>学无止境 - 新浪微博iOS底部功能按钮简单实现
- iOS开发>学无止境 - 保存照片到自己创建的相簿
- ios之自定义导航栏背景,标题和返回按钮文字颜色
- iOS开发>学无止境 - 遍历Model类的属性并完善使用Runtime给Model类赋值
- iOS开发>学无止境 - 6个iOS图片文本设计的小技巧
- iOS开发>学无止境 - 彩色TabBar切换动画实现
- iOS开发>学无止境 - 多线程简单介绍
- iOS开发>学无止境 - 方法缓存
- iOS开发>学无止境 - 异步图片加载优化与常用开源库分析
- iOS开发>学无止境 - Cell异步图片加载优化,缓存机制详解
- iOS开发>学无止境 - 自定义控制器转场动画及实现下拉菜单的小Demo
- iOS开发>学无止境 - AutoLayout 实现固定宽度动态高度的 ScrollView
- iOS开发>学无止境 - 全面了解 iOS 静态库开发
- iOS开发>学无止境 - POP 介绍与使用实践(快速上手动画)
- iOS开发>学无止境 - 简单介绍MVC
- iOS Core data多线程并发访问的问题