iOS实现手机的连续震动和停止震动的代码(拷如工程就能用)
来源:互联网 发布:win7软件停止运行 编辑:程序博客网 时间:2024/04/29 00:28
最近在做一个类似手机来电一样的让手机无线震动的功能。苹果官方给出的接口很简单总结起来就两步:
1.往项目中导入AudiToolbox.framework框架
2.就一句代码:
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
可以实现震动,但是就那么以下,我试了使用NStimer来实现不停的震动,可以实现,但是要暂停下来就麻烦了。
在网上搜了些资料,虽然说出了原理,但是具体的实现代码还是有点让人看不懂。以下是我自己总结的写出来的Demo代码,思路清晰简单,而且拷入工程就能用,不罗嗦直接上代码:
//// ViewController.m// 连续震动示例代码//// Created by vincent on 10/9/15.// Copyright © 2015 Mipow. All rights reserved.//#import "ViewController.h"//记得导入这个框架#import <AVFoundation/AVFoundation.h>@interface ViewController (){ SystemSoundID sound; NSTimer *shakeTimer;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //创建震动开始按钮 UIButton *startBtn_c=[[UIButton alloc]initWithFrame:CGRectMake(180, 200, 100, 44)]; startBtn_c.backgroundColor=[UIColor blueColor]; [startBtn_c setTitle:@"开始-C" forState:UIControlStateNormal]; [startBtn_c addTarget:self action:@selector(startButton_cClickedAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:startBtn_c]; //创建震动暂停按钮 UIButton *stopBtn_c=[[UIButton alloc]initWithFrame:CGRectMake(40, 200, 100, 44)]; stopBtn_c.backgroundColor=[UIColor redColor]; [stopBtn_c setTitle:@"暂停-C" forState:UIControlStateNormal]; [stopBtn_c addTarget:self action:@selector(stopButton_cClickedAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:stopBtn_c]; // Do any additional setup after loading the view, typically from a nib.}-(void)stopButton_cClickedAction{ NSLog(@"stop button action"); //[audioPlayer stop]; AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate); [self stopAlertSoundWithSoundID:sound];}-(void)stopAlertSoundWithSoundID:(SystemSoundID)sound { AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);}-(void)startButton_cClickedAction{ NSLog(@"start button action"); //如果你想震动的提示播放音乐的话就在下面填入你的音乐文件 NSString *path = [[NSBundle mainBundle] pathForResource:@"marbach" ofType:@"mp3"]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &sound); AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, soundCompleteCallback, NULL); AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); AudioServicesPlaySystemSound(sound);}void soundCompleteCallback(SystemSoundID sound,void * clientData) { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //震动 AudioServicesPlaySystemSound(sound);}extern OSStatusAudioServicesAddSystemSoundCompletion( SystemSoundID inSystemSoundID, CFRunLoopRef inRunLoop, CFStringRef inRunLoopMode, AudioServicesSystemSoundCompletionProc inCompletionRoutine, void* inClientData)__OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0);- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
以上代码能实现收放自如,想震动的时候不停的震动,想暂停的时候能立即停下!
0 0
- iOS实现手机的连续震动和停止震动的代码(拷如工程就能用)
- Android 手机震动的实现
- ios开发连续手机震动(kSystemSoundID_Vibrate连续震动)
- Android 震动 和 停止 代码
- IOS简单的实现手机震动的提示
- Android的手机震动
- Android手机震动抖动效果的实现
- Android中手机震动vibrate的实现
- Android手机的震动功能实现
- Android 手机震动功能的实现
- android 震动的实现
- 推送时,播放震动声音不停止的代码
- 一看就会Android之手机震动的设置
- iOS-使用AudioServices相关接口的连续震动
- 手机的声音与震动
- 调用手机的震动器
- iOS一行代码调用手机系统震动
- 使用AudioServices相关接口(底层c接口)的连续震动实现(ios)
- 图像处理&识别:应用
- linux下登录mysql
- 重构想法1
- 萧条时期的SEO要如何着手?
- 2.Python标准库_ 时间与日期 (time, datetime包)
- iOS实现手机的连续震动和停止震动的代码(拷如工程就能用)
- iOS 编译失败,但是没有错误提示(ios build failed ,but without errors)
- 使用word 2013 发布csdn博客
- VC++ MFC 常用技巧
- OC基础笔记
- redis php admin
- netty TCP server心跳机制
- ffmpeg 使用滤镜
- 在 XML 中通过数据绑定用一行代码定制字体