是否可以检测按钮的触摸?

时间:2011-04-08 12:16:17

标签: iphone objective-c xcode

我有一个应用程序,我想检测按钮上的触摸,如第一次触摸,第二次触摸。有可能吗?

2 个答案:

答案 0 :(得分:4)

这是一种方法..

UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(100, 100, 100, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];

然后在方法buttonPressed中你可以计算出触摸

-(void)buttonPressed
{
   touchCount++;
   // Do something or not
}

答案 1 :(得分:0)

只计算变量中的触摸事件。

相关问题