Maybe you're doing something unusual, like you only want touches to be detected within a triangle or something. If so, you can ignore this. But since you're new to iOS, I would recommend using either a UIControl
subclass with target-action pairs or a UIGestureRecognizer
.
Example code would be:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Later in the target
-- probably your view controller, you would implement the method:
- (void)buttonPressed:(id)sender
{
}
With a gesture recognizer, you would add the gesture recognizer to a UIView
and, like with the UIControl
, add a target-action pair:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[self.view addGestureRecognizer:tapRecognizer];
- (void)viewTapped:(UIGestureRecognizer *)recognizer
{
}
These are the typical approaches for basic user interaction. You'll have much simpler time with these than using touchesBegan
& company, though if they are what you need then the other answerers have you covered.