//单击
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleSingleTap:)];
[self.viewaddGestureRecognizer:singleTap];
//双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(handleDoubleTap:)];
//设置点击次数
[doubleTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTap];
//区别单击、双击
[singleTap requireGestureRecognizerToFail:doubleTap];
//创建一个手势识别器
swipeGestureRecognizer=[[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(swipeGesture:)]autorelease];
//设置几个手指
[swipeGestureRecognizer setNumberOfTouchesRequired:1];
//设置轻扫方向
// [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];
// [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
// [swipeGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
//设置代理
[swipeGestureRecognizer setDelegate:self];
//添加Gesture到view上
[self.view addGestureRecognizer:swipeGestureRecognizer];
//平移手势
panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:selfaction:@selector(panGesture:)];
[viewImg2 addGestureRecognizer:panGesture];
//长按手势
UILongPressGestureRecognizer *longGesture=[[UILongPressGestureRecognizeralloc]initWithTarget:self action:@selector(longGestureAction:)];
//设置长按时间
[longGesture setMinimumPressDuration:2];
[self.view addGestureRecognizer:longGesture];
//旋转手势
UIRotationGestureRecognizer *rotationGesture =
[[[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotationGestureAction:)] autorelease];
[viewImg1 addGestureRecognizer:rotationGesture];
//向里或向外捏的手势
UIPinchGestureRecognizer *twoFingerPinch =
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(clickH:)]autorelease];
[viewImg1 addGestureRecognizer:twoFingerPinch];
}
//单击监听
-(void)handleSingleTap:(UITapGestureRecognizer *)singleTap
{
NSLog(@"单击");
}
//双击监听
-(void)handleDoubleTap:(UITapGestureRecognizer *)doubleTap
{
NSLog(@"双击");
}
//轻扫监听
-(void)swipeGesture:(UISwipeGestureRecognizer *)swipeGesture
{
if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionUp){
NSLog(@"向上轻扫");
}
if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionDown){
NSLog(@"向下轻扫");
}
if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionLeft){
NSLog(@"向左轻扫");
}
if (swipeGestureRecognizer.direction & UISwipeGestureRecognizerDirectionRight){
NSLog(@"向右轻扫");
}
}
//平移监听
-(void)panGesture:(UIPanGestureRecognizer *)_panGesture
{
CGPoint point=[panGesture locationInView:self.view];
viewImg2.center=point;
NSLog(@"%@",NSStringFromCGPoint(point));
}
//长按监听
-(void)longGestureAction:(UILongPressGestureRecognizer *)longPress
{
//因为长按超过2秒后放开后会多打印一次,所以此处进行判定在它end状态时return ,不打印
if (longPress.state==UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"长按");
}
//旋转监听
-(void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation
{
//将弧度转成角度
float degree= rotation.rotation*(180/M_PI);
NSLog(@"%f",degree);
}
//-(void)clickH:(id)sender
//{
// UIView *view = [[self.view subviews] objectAtIndex:1];
// [self.view insertSubview:view atIndex:0];
// //将当前的view放到最底部。
//}