iOS中的手势详解

1、点击
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];

//设置当前需要点击的次数
[tap setNumberOfTapsRequired:1];
//设置当前需要触发事件的手指数量

[tap setNumberOfTouchesRequired:2];
//设置当前代理
tap.delegate=self;
[_view addGestureRecognizer:tap];
//触发方法
- (void) click{
NSLog(@”当前视图被点击了! “);
}
2、长按
UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
//设置当前长按最小的时长
longPress.minimumPressDuration=2;

//设置允许的移动范围
[longPress setAllowableMovement:2];
[_view addGestureRecognizer:longPress];
//触发方法
- (void) longPress{
NSLog(@”长按事件触发! “);
}
3、轻扫
UISwipeGestureRecognizer * swip=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipMethod)];
//往左边方向
swip.direction=UISwipeGestureRecognizerDirectionLeft ;
//往右边方向
swip.direction=UISwipeGestureRecognizerDirectionRight ;
//往上面方向
swip.direction=UISwipeGestureRecognizerDirectionUp ;
//往下面方向
swip.direction=UISwipeGestureRecognizerDirectionDown ;
[_view addGestureRecognizer:swip];

//触发方法
- (void) swipMethod{
    NSLog(@"轻扫事件触发! ");

}
如果涉及到2个以上方向的手势最好添加多个UISwipeGestureRecognizer 对象并设置不同的方向,不要通过下面方式用符号|来连接:
swip.direction=UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight
4、拖动

第一步:添加视图
_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];
[_view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_view];
第二步:添加手势
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(paned:)];
[_view addGestureRecognizer:pan];
第三步:实现方法
- (void) paned:(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];
}
5、旋转

第一步:添加视图
_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];
[_view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_view];
第二步:添加手势
UIRotationGestureRecognizer * roate=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[_view addGestureRecognizer:roate];
roate.delegate=self;
第三步:实现方法
- (void) rotate:(UIRotationGestureRecognizer *) rote{
//获取当前旋转的度数
CGFloat rotation= rote.rotation;
//通过仿射变换实现旋转
_view.transform=CGAffineTransformRotate(_view.transform, rotation);
//防止旋转叠加需要清零
rote.rotation=0;
}
6、捏合

第一步:添加视图
_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];
[_view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_view];
第二步:添加手势
UIPinchGestureRecognizer * pich=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(piches:)];
[_view addGestureRecognizer:pich];
pich.delegate=self;
第三步:实现方法
- (void) piches:(UIPinchGestureRecognizer *) pich{
//获取比例
CGFloat scale=pich.scale;
//通过仿射变换实现缩放
_view.transform=CGAffineTransformScale(_view.transform, scale, scale);
//防止比例叠加需要置为1
pich.scale=1;
}
【补充】如果需要同时响应多个手势需要重写代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer )otherGestureRecognizer{
return YES;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值