Ulvi
Ulvi

Reputation: 71

Is it possible to detect touches of a button?

I have an app and i want to detect touches on a button like first touch, second touch. Is it possible?

Upvotes: 0

Views: 288

Answers (2)

Jordan
Jordan

Reputation: 21760

Here's one way to do it..

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

Then in the method buttonPressed you can tally up the touches

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

Upvotes: 4

tamasgal
tamasgal

Reputation: 26259

Just count the touch events in a variable.

Upvotes: 0

Related Questions