美文网首页iOS开发知识汇总
手机持续震动的实现

手机持续震动的实现

作者: 梦想总是美好的 | 来源:发表于2017-02-22 13:27 被阅读6次

首先声明,这个方法是网上一个朋友发给我的。非自己原创。https://github.com/dustturtle/ 这是他的github地址。

首先,创建了单例。直接贴代码了。

ShakeInstance.h中:

#import#import@interface ShakeInstance : NSObject

//单例接口

+ (ShakeInstance *)shareInstance;

- (void)vibrateCallback;

- (void) triggerShake;

@end

ShakeInstance.m中:

#import "ShakeInstance.h"

@implementation ShakeInstance

static ShakeInstance *shakeInstance = nil;

//单例接口

+ (ShakeInstance *)shareInstance

{

if (!shakeInstance)

{

@synchronized(self)

{

if (!shakeInstance)

{

shakeInstance = [[ShakeInstance alloc] init];

}

}

}

return shakeInstance;

}

- (void)vibrateCallback

{

// 此处设置震动间隔

[self performSelector:@selector(triggerShake) withObject:nil afterDelay:1];//设置震动之间的间隔时间

}

- (void)triggerShake

{

SystemSoundID soundID = kSystemSoundID_Vibrate;

AudioServicesPlaySystemSound (soundID);

}

@end

ViewController.m中调用:

#import "ViewController.h"

#import "ShakeInstance.h"

@interface ViewController ()

- (IBAction)stopShake:(id)sender;

- (IBAction)startShake:(id)sender;

@end

@implementation ViewController

void systemAudioCallback(SystemSoundID soundId, void *clientData)

{

[[ShakeInstance shareInstance] vibrateCallback];

}

- (void)viewDidLoad

{

[super viewDidLoad];

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

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)stopShake:(id)sender

{

AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);//移除停止震动

[NSObject cancelPreviousPerformRequestsWithTarget:[ShakeInstance shareInstance]

selector:@selector(triggerShake)

object:nil];

}

- (IBAction)startShake:(id)sender

{

AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, systemAudioCallback, NULL);

SystemSoundID soundID = kSystemSoundID_Vibrate;

AudioServicesPlaySystemSound (soundID);

}

@end'

相关文章

  • 手机持续震动的实现

    首先声明,这个方法是网上一个朋友发给我的。非自己原创。https://github.com/dustturtle/...

  • swift实现持续震动

    首先我必须说清楚的是,本文的重点不是讲如何通过Swift实现持续震动这个功能,而是通过实现这个功能来学习在Swif...

  • iOS开发 实现手机震动

    不要搞成病毒,让手机一直震动 1.系统震动(整栋比较短) AudioServicesPlaySystemSound...

  • iOS简单实现震动

    简单实现手机震动,首先导入AudioToolBox.framework,在需要震动的文件中#import 。调用...

  • swift3.0+实现持续震动和取消震动

    只是简单的实现功能,使用GCD延迟循环调用,主要是swift对gcd写法的改变,可以写个单例,全局调用,方法内部可...

  • 手机震动

    我上周参加了一场面试,同场面试的还有两人,其中一人刚刚留学归来,另外一人已经工作过了一段时间。 留学归来的那个人身...

  • iOS开发 振动实现

    1.AudioServicesPlaySystemSound简单实现手机震动,首先导入AudioToolBox.f...

  • iOS开发中如何检测手机在摇一摇

    检测手机的震动需要导入 import 框架 然后实现下面...

  • iOS 手机铃声+连续震动效果的实现

    "从一个菜鸟演变成一个老鸟这个过程和漫长,只有在不断的碰磕中你才会成长,我已经遍体鳞伤了."就此打住,不要再发那些...

  • 安卓开发之实现手机震动

    其实要使安卓手机实现震动功能并困难,只需要调用os 包中的vibrator类就可以实现,这个包也是系统自带的,不需...

网友评论

本文标题:手机持续震动的实现

本文链接:https://www.haomeiwen.com/subject/lxfmwttx.html