点击
-(void)setTap
{
UITapGestureRecognizer*tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[self.view addGestureRecognizer:tap];
}
-(void)tap:(UITapGestureRecognizer*)tap
{
}
捏合
-(void)setUpPinch
{
UIPinchGestureRecognizer*pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[self.view addGestureRecognizer:pinch];
}
-(void)pinch:(UIPinchGestureRecognizer*)pinch
{
self.view.transform=CGAffineTransformScale(self.view.transform, pinch.scale, pinch.scale);
// 复位 由于手势是相对原始位置计算的旋转角度 复位可以试试保存
pinch.scale=1;
}
旋转
-(void)setUpRotation
{
UIRotationGestureRecognizer*rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[self.view addGestureRecognizer:rotation];
}
-(void)rotation:(UIRotationGestureRecognizer*)rotation
{
self.view.transform=CGAffineTransformRotate(self.view.transform, rotation.rotation);
// 复位 默认传递的旋转的角度都是相对于最开始的位置
rotation.rotation=1;
}
长按
//默认会触发两次
-(void)setUpLongPress
{
UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self.view addGestureRecognizer:longPress];
}
-(void)longPress:(UILongPressGestureRecognizer*)longPress
{
if (longPress.state==UIGestureRecognizerStateBegan)
{
NSLog(@"UIGestureRecognizerStateBegan");
}
else if (longPress.state==UIGestureRecognizerStateEnded)
{
NSLog(@"UIGestureRecognizerStateEnded");
}
}
轻扫
-(void)setUpSwipe
{
//默认轻扫的方向是往右
UISwipeGestureRecognizer*swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
swipe.direction=UISwipeGestureRecognizerDirectionUp;
swipe.delegate=self;//默认是不支持多个手势
[self.view addGestureRecognizer:swipe];
//如果想要一个控件支持多个方向的轻扫,必须创建多个轻扫手势,一个手势只支持一个方向
UISwipeGestureRecognizer*swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
swipeDown.direction=UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate=self;//默认是不支持多个手势
[self.view addGestureRecognizer:swipeDown];
}
-(void)swipe
{
NSLog(@"%s",__func__);
}
滑动
-(void)setUpPan
{
UIPanGestureRecognizer*pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer*)pan
{
//获取手势的触摸点
CGPoint locaP=[pan locationInView:self.view];
//获取手势的移动,也是相对于最开始的位置
CGPoint transP=[pan translationInView:self.view];
self.view.transform=CGAffineTransformScale(self.view.transform, transP.x, transP.y);
//复位
[pan setTranslation:CGPointZero inView:self.view];
NSLog(@"%@",NSStringFromCGPoint(transP));
}
代理
//是否允许开始触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
//是否允许同时支持多个手势,默认是不支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
//是否允许接收手指的触摸点
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
CGPoint curP=[touch locationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(curP));
return YES;
}