下面来一起学习下常见的手势:
UIPanGestureRecognizer(拖动)
UIPinchGestureRecognizer(捏合)
UIRotationGestureRecognizer(旋转)
UITapGestureRecognizer(点按)
UILongPressGestureRecognizer(长按)
UISwipeGestureRecognizer(轻扫)
1 、UISwipeGestureRecognizer 轻扫手势
UISwipeGestureRecognizer 我们有两个属性:numberOfTouchesRequired和direction
direction 是轻扫的方向默认UISwipeGestureRecognizerDirectionRight;
numberOfTouchesRequired 是轻扫手指的数量 默认是1;
代码使用
//轻扫
- (void)createSwipeGesture:(UIView *)view {
UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//轻扫手指的数量
= 1;
//轻扫的方向默认UISwipeGestureRecognizerDirectionRight
// = UISwipeGestureRecognizerDirectionRight;//向右
// = UISwipeGestureRecognizerDirectionUp;//向上
// = UISwipeGestureRecognizerDirectionLeft;//向左
= UISwipeGestureRecognizerDirectionDown;//向下
[view addGestureRecognizer:swipe];
}
- (void)swipeGesture:(UIGestureRecognizer *)swipe {
UISwipeGestureRecognizer * gesture = (UISwipeGestureRecognizer *)swipe;
if ( == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上轻扫");
} else if ( == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下轻扫");
} else if ( == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左轻扫");
} else {
NSLog(@"向右轻扫");
}
}
2 、UITapGestureRecognizer 点按手势
UITapGestureRecognizer 属性 numberOfTapsRequired和numberOfTouchesRequired
@property (nonatomic) NSUInteger numberOfTapsRequired; //点击次数 默认是1
@property (nonatomic) NSUInteger numberOfTouchesRequired//点击手指 默认是1
代码使用
//点按
- (void)createTapGesture:(UIView *)view {
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
= 1;
= 2;//双击
[view addGestureRecognizer:tap];
}
- (void)tapGesture:(UIGestureRecognizer *)tap {
UITapGestureRecognizer * gesture = (UITapGestureRecognizer *)tap;
NSLog(@"点击了%lu次触发",(unsigned long));
}
numberOfTapsRequired上面设置的是2 双击 ,单击的时候 并不会执行tapGesture 方法
3 、UILongPressGestureRecognizer 长按手势
UILongPressGestureRecognizer 长按也可以拖拽,常用的属性 minimumPressDuration、numberOfTouchesRequired、numberOfTapsRequired、allowableMovement
numberOfTapsRequired://长点击响应前点击次数,默认0;
numberOfTouchesRequired://用户触摸的手指数,默认1
minimumPressDuration://指定用户至少在屏幕上按下多少秒才会触发该长按手势。该默认值为0.5
allowableMovement//手指长按期间可移动的区域,默认10像素
代码使用
//长按
- (void)createLongGesture:(UIView *)view {
UILongPressGestureRecognizer * longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGesture:)];
= 0;//长点击响应前点击次数,默认0;
= 1;// 用户触摸的手指数,默认1
= 0.5;//长按最低时间,默认0.5秒
= 5;//手指长按期间可移动的区域,默认10像素
[view addGestureRecognizer:longGesture];
}
- (void)longGesture:(UILongPressGestureRecognizer *)longGesture {
if ( == UIGestureRecognizerStateBegan)
{
NSLog(@"开始");
}
else if ( == UIGestureRecognizerStateChanged)
{
NSLog(@"进行中");
}
else if ( == UIGestureRecognizerStateEnded)
{
NSLog(@"结束");
}
}
4、UIPanGestureRecognizer 拖拽手势
UIPanGestureRecognizer 属性
minimumNumberOfTouches//最小的拖动范围
maximumNumberOfTouches //最大拖动范围
方法
// 获取移动后相对与view的坐标系的偏移量
- (CGPoint)translationInView:(nullable UIView *)view;
// 获取移动后的坐标 - (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
代码实现
//拖动
- (void)createPanGesture:(UIView *)view {
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureEvent:)];
[view addGestureRecognizer:pan];
}
//拖动
- (void)gestureEvent:(UIGestureRecognizer *)sender {
UIView *btn = (UIView *);
if ( == UIGestureRecognizerStateBegan)
{
startPoint = [sender locationInView:];
originPoint = ;
}
else if ( == UIGestureRecognizerStateChanged)
{
CGPoint newPoint = [sender locationInView:];
CGFloat deltaX = ;
CGFloat deltaY = ;
= CGPointMake(+deltaX,+deltaY);
[UIView animateWithDuration:1 animations:^{
CGPoint temp = CGPointZero;
temp = ;
= temp;
originPoint = ;
}];
}
else if ( == UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:1 animations:^{
if ( - /2<=0) {
= /2 + 10;
}
if ( - /2 - 20 <=0) {
= /2 + 44 + 10;
}
if ( >= [UIScreen mainScreen].- /2) {
= [UIScreen mainScreen]. - /2 - 10;
}
if ( >= [UIScreen mainScreen].- /2) {
= [UIScreen mainScreen].- /2 - 10;
}
= originPoint;
}];
}
}
5 、UIPinchGestureRecognizer 捏合手势
UIPinchGestureRecognizer 的属性 有 scale 和 velocity:
scale //是捏合的比例;
velocity //捏合的速度
代码实现
//捏合
- (void)createPinchGesture:(UIView *)view {
//缩放手势
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePinch:)];
[view addGestureRecognizer:pinchGestureRecognizer];
}
-(void)handlePinch:(UIPinchGestureRecognizer *)sender
{
//scale 缩放比例
// = CGAffineTransformMake(, 0, 0, , 0, 0);
//每次缩放以原来位置为标准
// = CGAffineTransformMakeScale(, );
//每次缩放以上一次为标准
= CGAffineTransformScale(, , );
//重新设置缩放比例 1是正常缩放.小于1时是缩小(无论何种操作都是缩小),大于1时是放大(无论何种操作都是放大)
= 1;
}
6、UIRotationGestureRecognizer 旋转手势
UIRotationGestureRecognizer属性
@property (nonatomic) CGFloat rotation;//旋转角度
@property (nonatomic,readonly) CGFloat velocity;//旋转速度 只读
代码
//旋转
- (void)createRotationGesture:(UIView *)view {
UIRotationGestureRecognizer * rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[view addGestureRecognizer:rotationGesture];
}
- (void)rotationGesture :(UIRotationGestureRecognizer *) rotationGesture{
//捏合手势两种改变方式
//以原来的位置为标准
// = CGAffineTransformMakeRotation();//rotation 是旋转角度
//两个参数,以上位置为标准
= CGAffineTransformRotate(, );
//消除增量
= 0.0;
}
对应demo下载地址