iOS实现手机的连续震动和停止震动的代码(拷如工程就能用)

最近在做一个类似手机来电一样的让手机无线震动的功能。苹果官方给出的接口很简单总结起来就两步:

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 

@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 OSStatus
AudioServicesAddSystemSoundCompletion(  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

以上代码能实现收放自如,想震动的时候不停的震动,想暂停的时候能立即停下!


你可能感兴趣的:(iOS实现手机的连续震动和停止震动的代码(拷如工程就能用))