iPhone - vibrate 5 times?
In my application I want to be able to vibrate for a few seconds to alert the user something is taking place.
I have used the following code:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
And this works as in it will vibrate once.
However I want to be able to vibrate 5 times, not continuously or anything I just want to be able to vibrate once, stop, vibrate again, stop and so on, is this pos开发者_StackOverflowsible and acceptable by Apple via the app store?
That's currently the only method you can cause the phone to vibrate. But you could call that function 5 times!
You can do something like this..
Create an Integer. Create a 1 second timer. Hit a function every 1 second and make the phone vibrate while increasing the integer by 1. If the integer hits 5 kill the timer.
Doing it this way allows you to put a little space between vibrations so it will last as long as you'd like.
in your .h file
int vibrateCount;
NSTimer * vibrateTimer;
@property(nonatomic,assign)int vibrateCount;
@property (nonatomic, retain)NSTimer * vibrateTimer;
in your .m file
@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];
}
}
精彩评论