在ios开发中,系统自带了6种手势,分别是:
UIPinchGestureRecognizer 捏合
UILongPressGestureRecognizer 长按
UIRotationGestureRecognizer 旋转
UIPanGestureRecognizer 拖动
UISwipeGestureRecognizer 轻扫
UITapGestureRecognizer 点击
1.单击双击
//单击双击
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
[imageView addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;//需要点击的次数
[imageView addGestureRecognizer:doubleTap];
//双击响应时,单击需要失效
[singleTap requireGestureRecognizerToFail:doubleTap];
对应的方法
-(void)singleTapAction:(UITapGestureRecognizer *)sender{
NSLog(@"你点击了我");
}
-(void)doubleTapAction:(UITapGestureRecognizer *)sender{
NSLog(@"你双击了我");
}
2.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[imageView addGestureRecognizer:longPress];
对应的方法
-(void)longPressAction:(UILongPressGestureRecognizer *)sender{
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"你按了我很久");
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"保存图片" delegate:self cancelButtonTitle:@"确定" destructiveButtonTitle:@"下载图片" otherButtonTitles:@"保存到相册",@"打开相册",@"打开相机", nil];
[sheet showInView:self.view];
}
}
3.捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.view addGestureRecognizer:pinch];
对应的方法
-(void)pinchAction:(UIPinchGestureRecognizer *)sender{
NSLog(@"正在捏合");
// imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale);
imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
}
4.旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[self.view addGestureRecognizer:rotation];
[pinch requireGestureRecognizerToFail:rotation];
对应的方法
-(void)rotationAction:(UIRotationGestureRecognizer *)sender{
NSLog(@"正在旋转");
// imageView.transform = CGAffineTransformRotate(imageView.transform, sender.rotation);
imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
}
5.拖动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
对应的方法
-(void)panAction:(UIPanGestureRecognizer *)sender{
//转换坐标系
CGPoint point = [sender translationInView:self.view];
//移动
imageView.center = CGPointMake(imageView.center.x+point.x, imageView.center.y+point.y);
//
[sender setTranslation:CGPointZero inView:self.view];
}
6.轻扫
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:rightSwipe];
对应的方法
-(void)swipeAction:(UISwipeGestureRecognizer *)sender{
switch (sender.direction) {
case UISwipeGestureRecognizerDirectionLeft:
{
NSLog(@"你向左轻扫");
count++;
if (count > images.count-1) {
[[[UIAlertView alloc] initWithTitle:@"" message:@"已经是最后一张" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
count--;
return;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:imageView cache:YES];
[UIView commitAnimations];
imageView.image = images[count];
}
break;
case UISwipeGestureRecognizerDirectionRight:
{
NSLog(@"你向右轻扫");
count--;
if (count < 0) {
[[[UIAlertView alloc] initWithTitle:@"" message:@"已经是第一张了" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
count++;
return;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:imageView cache:YES];
[UIView commitAnimations];
imageView.image = images[count];
}
break;
default:
break;
}
}