该方法为您提供了屏幕上所有触摸的 NSSet。您可以使用它来自定义您的触摸行为。例如:
//This will change the state of whether mainToolbar is hidden or not. In the case of multiple touches, it will change the property if any touch is in the CGRect area.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect testRect = CGRectMake(130, 0, 60, 480);
for (UITouch *touch in touches) {
if (CGRectContainsPoint(testRect, [touch locationInView:self.view])) {
mainToolbar.hidden = !mainToolbar.hidden;
}
}
}
或者,如果您只希望在进行一次触摸时发生该操作,而在用户用多个手指触摸时忽略它,您将检查:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect testRect = CGRectMake(130, 0, 60, 480);
if ([touches count] == 1) {
//If there is only one touch, we check for that. Otherwise, we ignore it.
UITouch *touch = [touches anyObject];
if (CGRectContainsPoint(testRect, [touch locationInView:self.view])) {
mainToolbar.hidden = !mainToolbar.hidden;
}
}
}
你可以用这四个函数touchesBegan:
touchesMoved:
touchesEnded:
和touchesCanceled:
.