UI 常用方法总结之---各个手势种类 UIGestureRecognizer

本文详细介绍了iOS中常见的六种手势识别器:点击、长按、清扫、旋转、拖拽及捏合,每种手势识别器都包括其创建方式、关键属性及绑定方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手势种类

UIGestureRecognizer : NSObject

1.- (void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer

附加一个手势识别器到视图。

一.点击 tap

UITapGestureRecognizer : UIGestureRecognizer

1.创建一个UITapGestureRecognizer对象

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

2.numberOfTapsRequired

按几次触发

eg:tap.numberOfTapsRequired = 2;

3.numberOfTouchesRequired

几根手指触发

eg:tap.numberOfTouchesRequired = 2;

 

二.长按 longpress

UILongPressGestureRecognizer : UIGestureRecognizer

1.创建一个UILongPressGestureRecognizer对象

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];

2.minimumPressDuration

判定长按 需要的时间 默认为0.5s

eg:longPress.minimumPressDuration = 1;

3.allowableMovement

允许长按过程中移动的像素 默认10

eg:longPress.allowableMovement = 100;

4.常用绑定方法

    if(longPress.state == UIGestureRecognizerStateBegan)

    {

        NSLog(@"长按");

    

    }

 

 

三.清扫 swipe

UISwipeGestureRecognizer : UIGestureRecognizer

 

1.创建一个UISwipeGestureRecognizer对象

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];

2.direction

滑动方向

eg:swipe.direction = UISwipeGestureRecognizerDirectionLeft;

3.numberOfTouchesRequired

几根手指触发

 

 

 

四.旋转 rotation

UIRotationGestureRecognizer : UIGestureRecognizer

1.创建一个UIRotationGestureRecognizer对象

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

2.rotation

旋转速度

3.常用绑定方法

    NSLog(@"旋转");

    //获得当前手势所在的view

    UIView *view = rotation.view;

    

    //transform属性

    view.transform = CGAffineTransformRotate(view.transform, rotation.rotation);

 

    rotation.rotation = 0;

 

 

五.拖拽 pan

UIPanGestureRecognizer : UIGestureRecognizer

1.创建一个UIPanGestureRecognizer对象

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

2.常用绑定方法

    NSLog(@"拖拽");

    CGPoint translation = [pan translationInView:self.view];

    UIView *view = pan.view;

    view.center = CGPointMake(view.center.x + translation.x, view.center.y + translation.y);

 

    [pan setTranslation:CGPointMake(0, 0) inView:self.view];

 

 

 

六.捏合 pinch

UIPinchGestureRecognizer : UIGestureRecognizer

1.创建一个UIPinchGestureRecognizer对象

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];

2.常用绑定方法

    UIView *view = pinch.view;

 

    view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);

3.scale

比例

eg:pinch.scale = 1;

 

 

七.屏幕边缘拖拽 screenEdgePan

UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer

1.创建一个UIScreenEdgePanGestureRecognizer对象

UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePan:)];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值