美文网首页
Objective-C ios手势识别

Objective-C ios手势识别

作者: 影子的秘密 | 来源:发表于2018-07-29 17:34 被阅读16次

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);
}

相关文章

  • 手势识别

    手势识别 6种手势识别 在iOS开发中有6中手势识别:点按、捏合、拖动、轻扫、旋转、长按苹果推出手势识别主要是为了...

  • Objective-C ios手势识别

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

  • iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

  • 笔记篇 之 "手势"

    iOS开发中手势识别有六种: 轻击手势(TapGestureRecognizer),轻扫手势 (SwipeGest...

  • iOS开发中六种手势识别

    iOS开发中手势识别有六种: 轻击手势(TapGestureRecognizer), 轻扫手势 (SwipeGes...

  • iOS手势识别器

    1.手势识别器 1.手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓手势:有规律的触摸。是对触摸事件做...

  • iOS-手势UIGestureRecognier详解

    一. 手势UIGestureRecognier简介 iOS 3.2之后,苹果推出了手势识别功能(Gesture R...

  • iOS 事件以及手势的处理

    iOS 事件以及手势的处理 首先引用深入浅出iOS事件机制,iOS触摸事件处理详解,详解iOS触摸事件与手势识别三...

  • ios手势识别

    使用手势识别 六种手势识别(继承于UIGestureRecongnizer基类): UITapGestureRec...

  • iOS手势识别

    UIGestureRecognizer手势识别器手势识别器是特殊的触摸事件UIGestureRecognizer是...

网友评论

      本文标题:Objective-C ios手势识别

      本文链接:https://www.haomeiwen.com/subject/xxctvftx.html