iPhone - 振动5次?

Don*_*rty 3 objective-c ios

在我的应用程序中,我希望能够振动几秒钟以提醒用户正在发生的事情.

我使用了以下代码:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)

而这就像它会振动一次.

然而,我希望能够振动5次,而不是连续振动或任何我只想振动一次,停止,再次振动,停止等等,苹果通过应用程序商店可以接受吗?

Jam*_*ter 9

这是目前唯一可以让手机振动的方法.但你可以调用该功能5次!

  • `while(true){AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); };`;) (6认同)

Lou*_*uie 9

你可以这样做..

创建一个整数.创建1秒计时器.每1秒击中一个功能,使手机振动,同时将整数增加1.如果整数命中5则杀死计时器.

这样做可以让你在振动之间留出一点空间,这样它就会持续你想要的时间.

在你的.h文件中

int vibrateCount;
NSTimer * vibrateTimer;

@property(nonatomic,assign)int vibrateCount;
@property (nonatomic, retain)NSTimer * vibrateTimer;
Run Code Online (Sandbox Code Playgroud)

在.m文件中

@synthesize vibrateCount,vibrateTimer;

//in viewDidLoad
vibrateCount = 0;

//put this where ever you want to start your vibrating
vibrateTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(vibratePhone) userInfo:nil repeats:YES];


-(void)vibratePhone {
vibrateCount = vibrateCount +1;

if(vibrateCount <= 5) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
else {

      //vibrated 5 times already kill timer and stop vibrating
      [vibrateTimer invalidate];

   }
}
Run Code Online (Sandbox Code Playgroud)