該方法爲您提供了屏幕上所有觸摸的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:
就可以做一些非常酷的事情。
你把這段代碼放在哪裏?你有沒有繼承你的工具欄? – Toploulou
是的,我已經從子類 – Denny
採取工具欄,那麼touchBegan方法將只在工具欄中有效。換句話說,只有當用戶觸摸工具欄時它纔會起作用。 – Toploulou