点击计数器app在特定数量的点击后振动

时间:2014-01-12 22:22:22

标签: ios objective-c xcode count

我正在创建一个应用程序,其中一部分有一个点击计数器 用户界面包括一个按下按钮进行计数的按钮,一个重置按钮和一个显示水龙头数量的标签。问题是我希望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"];
}

当计数器达到预定值时,如何让应用程序振动或发出声音?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

好好让它停止响应点击,只需做一个if声明

例如,如果您希望在5次点击后停止,则为f。

-(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];
}

要做振动,看看Wain的回答。要播放声音,请查看AVAudioPlayer

答案 1 :(得分:0)

创建按钮的插座并在达到计数限制时将其禁用(因此它不会再响应触摸)。然后在重置时再次启用它。

振动:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);