我正在创建一个应用程序,其中一部分有一个点击计数器。
用户界面由一个按下计数按钮、一个重置按钮和一个显示点击量的标签组成。问题是我希望iDevice在特定点击后振动或发出声音,然后在那里结束,这样计数器标签就不会再对按钮做出反应。
这是我到目前为止的代码:
.h
@interface TapsViewController : UIViewController {
int counter;
IBOutlet UILabel *count;
}
-(IBAction)plus;
-(IBAction)reset;
@end
.m
- (void)viewDidLoad {
counter=0;
count.text = @"Start";
}
-(IBAction)plus{
counter=counter + 1;
count.text = [NSString stringWithFormat:@"%i", counter];
}
-(IBAction)reset{
counter=0;
count.text = [NSString stringWithFormat:@"Start"];
}
如何让应用程序在计数器达到预定值时振动或发出声音?
感谢您的帮助!
好吧,要使其停止响应水龙头,只需执行if
语句
例如,如果您希望它在点击 5 次后停止。
-(IBAction)plus{
if (counter < 4) {
//just a tip, counter++; does the same thing as counter += 1; which does the same thing as counter = counter+1;
counter++;
}
else if (counter == 4) {
//after four taps, counter will equal 4, so this part will be called on the 5th tap.
counter++;
//play your sound or do your vibrate here
}
count.text = [NSString stringWithFormat:@"%i",counter];
}
要做振动,看看韦恩的答案。 要播放声音,请查看AVAudioPlayer
。
为按钮创建一个插座,并在达到计数限制时禁用它(这样它就不会再响应触摸了)。然后在重置时再次启用它。
要振动:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);