觸摸事件和手勢

1、UIView自帶有的觸摸事件html

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.tapCount == 2) // 雙擊
            NSLog(@"++++++++++++++++++++++++++++++++++++++++%lu", [touch tapCount]);
    if (event.allTouches.count == 2) // 兩根手機觸摸
            NSLog(@"++++++++++++++++++++++++++++++++++++++++%lu", [event.allTouches count]);
}

只需實現該事件函數便可。可是,注意,如果在UIViewController裏有一個UITableView,點擊該UITableView,觸摸事件是會被UITableView截斷的函數

參考連接:http://www.cnblogs.com/syxchina/archive/2012/10/14/2723541.htmlui

2、手勢UITapGestureRecognizerspa

UITapGestureRecognizer能夠添加到任何的UIView控件上code

// 默認是單指單機觸發
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]   initWithTarget:self action:@selector(dismissKeyboard:)];
[self.view addGestureRecognizer:tap];
// tap.numberOfTapsRequired = 1; // 單擊數
// tap.numberOfTouchesRequired = 1; // 手指數
...
- (void)dismissKeyboard:(UITapGestureRecognizer *)tap 
{
    // TODO
}
相關文章
相關標籤/搜索