IOS 获取UIView触摸响应事件

来源:互联网 发布:关于创业的软件 编辑:程序博客网 时间:2024/05/17 07:42

有两种方法

1、注册手势

用  UIGestureRecognizer  的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。 UIGestureRecognizer 的衍生类別有以下几种:
    •    UITapGestureRecognizer
    •    UIPinchGestureRecognizer
    •    UIRotationGestureRecognizer
    •    UISwipeGestureRecognizer
    •    UIPanGestureRecognizer
    •    UILongPressGestureRecognizer

从命名上不难了解這些类別所对应代表的手势,分別是 Tap (点一下)、 Pinch (二指往內或往外拨动)、 Rotation (旋转)、 Swipe (滑动,快速移动)、 Pan (拖移,慢速移动)以及 LongPress (长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。
         

2、重新UIView触摸响应事件

- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event
{
    [ super touchesBegan :touches withEvent :event];

}
- ( void )touchesMoved:( NSSet *)touches withEvent:( UIEvent *)event
{
    [ super touchesMoved :touches withEvent :event];
}
- ( void )touchesEnded:( NSSet *)touches withEvent:( UIEvent *)event
{
    [ super touchesEnded :touches withEvent :event];
}

  当手指接触屏幕时,就会调用 touchesBegan:withEvent 方法;
当手指在屏幕上移时,动就会调用 touchesMoved:withEvent 方法;
 当手指离开屏幕时,就会调用 touchesEnded:withEvent 方法;
//

注意:采用重写方式,不要用手势否则会获取不到touchesEnded事件。