- 点击 UITapGestureRecognizer
- 长按 UILongPressGestureRecognizer
- 轻扫 UISwipeGestureRecognizer
- 拖拽 UIPanGestureRecognizer
- 旋转 UIRotationGestureRecognizer
- 捏合缩放 UIPinchGestureRecognizer
- 详细代码如下:
- #import "ViewController.h"
- @interface ViewController () <UIGestureRecognizerDelegate>
- {
- UIView * _view;
- }
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //创建视图
- _view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
- _view.backgroundColor=[UIColor blueColor];
- _view.center=self.view.center;
- [self.view addSubview:_view];
- //点击
- UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
- //当前需要点击次数
- tap.numberOfTapsRequired=2;
- //[tap setNumberOfTapsRequired:2]; //同上
- //设置当前出发时间需要的手指数
- tap.numberOfTouchesRequired=2; // option键 模拟手指
- //[tap setNumberOfTouchesRequired:2]; //同上
- [_view addGestureRecognizer:tap];
- //长按
- UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClick)];
- //在长按过程中移动10个单位也算长按
- longPress.allowableMovement=10;
- //[longPress setAllowableMovement:10]; //同上
- //长按的最短时间
- longPress.minimumPressDuration=2;
- [_view addGestureRecognizer:longPress];
- //轻扫
- UISwipeGestureRecognizer * swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction)];
- //往右边的方向 ——>
- swipe.direction=UISwipeGestureRecognizerDirectionRight;
- [_view addGestureRecognizer:swipe];
- //轻扫
- UISwipeGestureRecognizer * swipe2=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction2)];
- //往左边的方向 <——
- swipe2.direction=UISwipeGestureRecognizerDirectionLeft;
- [_view addGestureRecognizer:swipe2];
- //轻扫
- UISwipeGestureRecognizer * swipe3=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction3)];
- //往上边的方向 ⬆️
- swipe3.direction=UISwipeGestureRecognizerDirectionUp;
- [_view addGestureRecognizer:swipe3];
- //轻扫
- UISwipeGestureRecognizer * swipe4=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction4)];
- //往下边的方向 ⬇️
- swipe4.direction=UISwipeGestureRecognizerDirectionDown;
- [_view addGestureRecognizer:swipe4];
- //拖拽
- UIPanGestureRecognizer * pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
- [_view addGestureRecognizer:pan];
- //旋转
- UIRotationGestureRecognizer * rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
- rotation.delegate=self;
- [_view addGestureRecognizer:rotation];
- //捏合缩放
- UIPinchGestureRecognizer * pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
- pinch.delegate=self;
- [_view addGestureRecognizer:pinch];
- }
- #pragma mark - 点击
- - (void) tapClick
- {
- NSLog(@"点击");
- }
- #pragma mark - 长按
- - (void)longPressClick
- {
- NSLog(@"长按");
- }
- #pragma mark - 向右滑
- - (void) swipeAction
- {
- NSLog(@"right");
- }
- #pragma mark - 向左滑
- - (void) swipeAction2
- {
- NSLog(@"left");
- }
- #pragma mark - 向上滑
- - (void) swipeAction3
- {
- NSLog(@"up");
- }
- #pragma mark - 向下滑
- - (void) swipeAction4
- {
- NSLog(@"down");
- }
- #pragma mark - 拖拽移动
- - (void) panAction:(UIPanGestureRecognizer *) pan
- {
- //获取移动的大小
- CGPoint point=[pan translationInView:pan.view];
- //更改视图的中心点坐标
- CGPoint points=_view.center;
- points.x+=point.x;
- points.y+=point.y;
- _view.center=points;
- //每次都清空一下,消除坐标叠加
- [pan setTranslation:CGPointZero inView:pan.view];
- }
- #pragma mark - 旋转
- - (void) rotation:(UIRotationGestureRecognizer *) rote
- {
- //获取当前旋转的度数
- CGFloat rotation=rote.rotation;
- //通过仿射变换实现旋转
- _view.transform=CGAffineTransformRotate(_view.transform, rotation);
- //防止旋转叠加,清零
- rote.rotation=0;
- }
- #pragma mark - 缩放捏合
- - (void) pinch:(UIPinchGestureRecognizer *) pinch
- {
- //获取比例
- CGFloat scale=pinch.scale;
- //通过仿射变换实现缩放
- _view.transform=CGAffineTransformScale(_view.transform, scale, scale);
- //防止比例叠加
- pinch.scale=1;
- }
- #pragma mark - 代理方法实现旋转 + 缩放捏合 可同时进行
- - (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
- {
- return YES;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- 需要注意点:
- 1.键盘上的option键模拟手指
- 2.对同一个view来说,拖拽时,向左向右向上向下的手势失效。
来源: http://www.phpxs.com/code/1005476/