iOS 手势识别器

手势识别器

手势识别器介绍

手势识别器是对触摸事件做了封装,我们无需⾃己去判断某个手势 是否触发,手势识别器本⾝起到了识别作用,我们把重⼼放在识别之 后要做什么操作上面

手势识别器有7个子类: 分别是①轻拍手势;②平移手势;③轻扫手势;④缩放手势⑤旋转手势⑥长按手势⑦屏幕边界平移手势

//创建视图添加到根视图
UIImageView *redView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 200, 200)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
[redView release];

手势对象的定义方法相同, 以下以轻拍手势为例,作一个示例:

//1. 创建轻拍手势对象
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
//2. 将手势添加到视图上
[redView addGestureRecognizer:tapGesture];
//3. 释放
[tapGesture release];

1 . 轻拍手势 – UITapGestureRecognizer

//属性
@property (nonatomic) NSUInteger  numberOfTapsRequired;       // Default is 1. The number of taps required to match (设置轻拍次数, 默认为1)
@property (nonatomic) NSUInteger  numberOfTouchesRequired;    // Default is 1. The number of fingers required to match (设置触摸的点数, 默认为1)

2 . 平移手势 – UIPanGestureRecognizer

@property (nonatomic) NSUInteger numberOfTapsRequired;      // Default is 0. The number of full taps required before the press for gesture to be recognized (需要平移的次数)
@property (nonatomic) NSUInteger numberOfTouchesRequired;   // Default is 1. Number of fingers that must be held down for the gesture to be recognized (需要平移的手指的个数)

@property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.5. Time in seconds the fingers must be held down for the gesture to be recognized (需要平移的时间, 默认为0.5s)
@property (nonatomic) CGFloat allowableMovement;           // Default is 10. Maximum movement in pixels allowed before the gesture fails. Once recognized (after minimumPressDuration) there is no limit on finger movement for the remainder of the touch tracking (允许能够移动的距离, 默认为10像素)

3 . 轻扫手势 – UISwipeGestureRecognizer

@property(nonatomic) NSUInteger                        numberOfTouchesRequired; // default is 1. the number of fingers that must swipe (需要轻扫的手指的个数)
@property(nonatomic) UISwipeGestureRecognizerDirection direction; // default is UISwipeGestureRecognizerDirectionRight. the desired direction of the swipe. multiple directions may be specified if they will result in the same behavior (for example, UITableView swipe delete) ( 轻扫方向, 默认为向右)

4 . 缩放手势 – UIPinchGestureRecognizer

@property (nonatomic)          CGFloat scale;               // scale relative to the touch points in screen coordinates (相对在屏幕坐标上的触点的比例, 相对于上一次)
@property (nonatomic,readonly) CGFloat velocity;            // velocity of the pinch in scale/second (缩放速率, 只读属性)

5 . 旋转手势 – UIRotationGestureRecognizer

@property (nonatomic)          CGFloat rotation;            // rotation in radians (旋转的角度,相对于上一次)
@property (nonatomic,readonly) CGFloat velocity;            // velocity of the pinch in radians/second (缩放速率, 只读属性)

6 . 长按手势 – UILongPressGestureRecognizer

@property (nonatomic) NSUInteger numberOfTapsRequired;      // Default is 0. The number of full taps required before the press for gesture to be recognized (设置长按的次数)
@property (nonatomic) NSUInteger numberOfTouchesRequired;   // Default is 1. Number of fingers that must be held down for the gesture to be recognized (设置触摸的点数)

@property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.5. Time in seconds the fingers must be held down for the gesture to be recognized (设置最小按压持续时间, 默认是0.5)
@property (nonatomic) CGFloat allowableMovement;           // Default is 10. Maximum movement in pixels allowed before the gesture fails. Once recognized (after minimumPressDuration) there is no limit on finger movement for the remainder of the touch tracking (能够允许移动的像素, 默认是10)

7 . 屏幕边界平移手势 – UIScreenEdgePanGestureRecognizer

@property (readwrite, nonatomic, assign) UIRectEdge edges; // The edges on which this gesture recognizes, relative to the current interface orientation (设置平移的屏幕边缘)

在视图控制器中定义处理方法, 如下:

//轻拍
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
    tapGesture.view.backgroundColor = [UIColor randomColor];//设置背景颜色
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值