iOS-手势识别


ios系统提供了一些常用的手势(UIgestureRecognizer的子类),方便我们直接使用。


UIGestureRecognizer的继承关系:

6种手势中只有UITapGestureRecognizer是离散型手势
  • 离散型手势特点:

一旦识别就无法取消,而且只会调用一次手势操作事件(就是初始化手势时指定的回调方法)。

  • 连续性手势特点:

另外五种是连续性手势,他们会多次调用手势操作事件,而且在连续手势识别后可以取消手势。


添加手势识别的步骤

1.创建手势识别对象实例
创建时,指定一个回调方法,当手势开始,改变、或结束时,执行回调方法。

2.设置手势识别器对象实例的相关属性
这部分不是必须编写的。

3.将手势识别添加到对应的视图中
每个手势只对应一个 View,当用户触摸的范围在 View 的边界内时,如果手势和预定的一样,那就会执行回调方法。


点击手势TapGestureRecognizer

  • 新建tap手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
  • 设置点击次数和点击手指数
    这里可以控制用户的单击或者是双击
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
  • 将手势添加到对应视图
    [self.view addGestureRecognizer:tapGesture];
  • 点击触发的方法
-(void)tapGesture:(id)sender
{
    //点击后要做的事情        
}

长按手势LongPressGestureRecognizer

  • 新建长按手势
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
  • 设置长按时间
    longPressGesture.minimumPressDuration = 0.5; //(2秒)
  • 添加长按手势到对应视图
    [self.view addGestureRecognizer:longPressGesture];
  • 长按触发的方法
-(void)longPressGesture:(id)sender{
     //长按后要做的事情
 }

轻扫手势SwipeGestureRecognizer

  • 添加轻扫手势
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
  • 设置轻扫的方向
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默认向右
  • 添加轻扫手势到对应视图
    [self.view addGestureRecognizer:swipeGesture];
  • 轻扫触发的方法
-(void)swipeGesture:(id)sender{ 
    //轻扫后要做的事情
}

捏合手势PinchGestureRecognizer

  • 添加捏合手势
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
  • 将捏合手势添加到对应视图
[self.view addGestureRecognizer:pinchGesture];
  • 捏合触发的方法
-(void)pinchGesture:(id)sender{
    //捏合后要做的事
}

拖动手势PanGestureRecognizer

  • 添加拖动手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
  • 将拖动手势添加到对应视图
[self.view addGestureRecognizer:panGesture];
  • 拖动触发的方法
-(void)panGesture:(id)sender{
    //拖动后要做的事
}

旋转手势RotationGestureRecognizer

  • 添加旋转手势
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
  • 将旋转手势添加到对应视图
[self.view addGestureRecognizer:rotationGesture];
  • 旋转触发的方法
-(void)rotationGesture:(id)sender{
    //旋转后要做的事
}

demo:http://pan.baidu.com/s/1kVplmCZ

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容