Objective-C ios手势识别

iOS手势

  1. 支持的手势:Tap点击, Edge Pan边缘滑动, Swipe轻扫, Pinch放大缩小,Long Press长按手势, Pan滑动

创建Tap点击手势事件

// 创建一个tap点击对象实例并初始化, action是事件点击触发的函数;
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture)];

// 视图添加tap点击事件
[self.view addGestureRecognizer:tapRecognizer];

创建长按手势事件

// 创建长按手势事件对象实例并初始化
UILongPressGestureRecognizer* longPressRecogninzer = [[UILongPressGestureRecognizer alloc]
                                                       initWithTarget:self action:@selector(longPressGesture)];
// 绑定到指定视图来响应手势
[self.view addGestureRecognizer:longPressRecogninzer];

创建轻扫滑动手势识别

// 创建滑动手势识别对象实例并初始化
UISwipeGestureRecognizer* swipeRecognizer = [[UISwipeGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(swipeGesture)];
// 指定识别向上滑动触发手势识别
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;

// 绑定到指定视图来响应手势
[self.view addGestureRecognizer:swipeRecognizer];

创建滑动手势识别

// 创建滑动手势识别对象实例并初始化
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                action:@selector(panGesture:)];
// 绑定到指定视图来响应手势
[self.view addGestureRecognizer:panRecognizer];

// 创建响应事件触发的函数
- (void)panGesture:(UIPanGestureRecognizer*) sender {
  // 获取滑动的坐标
  CGPoint point = [sender locationInView:self.view];
  // 获取滑动手势的速度
  CGPoint speed = [sender velocityInView:self.view];
  // 输出获取到的信息
  NSLog(@"Point: %f,%f  Speed:%f,%f", point.x,point.y speed.x,speed.y);

}

创建屏幕边缘滑动手势识别

// 创建屏幕边缘滑动手势识别对象实例并初始化
UIScreenEdgePanGestureRecognizer* screenEdgePanRecognizer = [[UIScreenEdgePanGestureRecognizer alloc]
                                                              initWithTarget:self action:@selector(screenEdgePanGesture:)];

// 指定识别右边的边缘滑动
screenEdgePanRecognizer.edges = UIRectEdgeRight;

// 绑定到指定视图来响应手势
[self.view addGestureRecognizer:screenEdgePanRecognizer];

// 创建响应事件触发的函数

- (void) screenEdgePanGesture:(UIScreenEdgePanGestureRecognizer*) sender {
  if (sender.state == UIGestureRecognizerStateBegan) {
    NSLog(@"屏幕边缘滑动手势触发开始。");
  } else if (sender.state == UIGestureRecognizerStateEnded) {
    NSLog(@"屏幕边缘滑动手势触发结束");
  }
}

创建缩放手势识别

// 创建缩放手势识别对象实例和初始化
UIPinchGestureRecognizer* pinchRecognizer = [[UIPinchGestureRecognizer alloc]
                                              initWithTarget:self action:@selector(pinchGesture:)];
// 绑定到指定视图来响应手势
[self.view addGestureRecognizer:pinchRecognizer];

// 响应事件,输出缩放比例的大小数值
- (void) pinchGesture:(UIPinchGestureRecognizer*) sender {
  CGFloat scale = sender.scale;
  NSLog(@"pinch scale:%f", scale);
}

创建旋转手势识别

//
UIRotationGestureRecognizer* rotationRecognizer = [[UIRotationGestureRecognizer alloc]
                                                    initWithTarget:self action:@selector(rotationGesture:)];
//
[self.view addGestureRecognizer:rotationRecognizer];

//
- (void) rotationGesture:(UIRotationGestureRecognizer*) sender {
  CGFloat rotation = sender.rotation;
  NSLog(@"rotation 角度为: %f", rotation);
}

你可能感兴趣的:(Objective-C ios手势识别)