點擊
-(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);
// 復(fù)位 由于手勢是相對原始位置計算的旋轉(zhuǎn)角度 復(fù)位可以試試保存
pinch.scale=1;
}
旋轉(zhuǎn)
-(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);
// 復(fù)位 默認(rèn)傳遞的旋轉(zhuǎn)的角度都是相對于最開始的位置
rotation.rotation=1;
}
長按
//默認(rèn)會觸發(fā)兩次
-(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
{
//默認(rèn)輕掃的方向是往右
UISwipeGestureRecognizer*swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
swipe.direction=UISwipeGestureRecognizerDirectionUp;
swipe.delegate=self;//默認(rèn)是不支持多個手勢
[self.view addGestureRecognizer:swipe];
//如果想要一個控件支持多個方向的輕掃铃慷,必須創(chuàng)建多個輕掃手勢秽褒,一個手勢只支持一個方向
UISwipeGestureRecognizer*swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe)];
swipeDown.direction=UISwipeGestureRecognizerDirectionDown;
swipeDown.delegate=self;//默認(rèn)是不支持多個手勢
[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);
//復(fù)位
[pan setTranslation:CGPointZero inView:self.view];
NSLog(@"%@",NSStringFromCGPoint(transP));
}
代理
//是否允許開始觸發(fā)手勢
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
//是否允許同時支持多個手勢拳氢,默認(rèn)是不支持多個手勢
- (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;
}