触摸事件和手势

一、UIView自带有的触摸事件

- (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.html

二、手势UITapGestureRecognizer

UITapGestureRecognizer可以添加到任何的UIView控件上

// 默认是单指单机触发
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]   initWithTarget:self action:@selector(dismissKeyboard:)];
[self.view addGestureRecognizer:tap];
// tap.numberOfTapsRequired = 1; // 单击数
// tap.numberOfTouchesRequired = 1; // 手指数
...
- (void)dismissKeyboard:(UITapGestureRecognizer *)tap 
{
    // TODO
}

 

posted @ 2014-10-19 15:44  纠纠结结  阅读(158)  评论(0编辑  收藏  举报