- UITapGestureRecognizer 点击
- UIPinchGestureRecognizer 捏合
- UIRotationGestureRecognizer 旋转
- UISwipeGestureRecognizer 清扫(滑动)
- UIPanGestureRecognizer 拖拽
- UILongPressGestureRecognizer 长按
#pragma mark -
#pragma mark 创建视图(来选择进行那一个手势)
- (void)createSubViews
{
//创建一个imageView来显示一张图片
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 260, 300)];
[self.imageView setImage:[UIImage imageNamed:@"1.png"]];
self.imageView.userInteractionEnabled = YES;
[self.view addSubview:self.imageView];
[self.imageView release];
// 点击,长按,清扫,捏合,旋转,拖拽
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"点击", @"长按", @"清扫", @"捏合", @"旋转", @"拖拽", nil];
//创建一个UISegmentedControl控制器
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:arr];
// 添加点击事件时,注意触发事件的状态
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
seg.frame = CGRectMake(20, 400, 270, 30);
[self.view addSubview:seg];
[seg release];
}
- (void)segAction:(id)sender
{
UISegmentedControl *seg = (UISegmentedControl *)sender;
// 为了防止一个view上有多个手势而无法确定是哪一个手势
// 每次触发事件时,移除imageView的所有手势,重新添加新的手势
// 1.获得一个视图的所有手势
NSArray *gesture = self.imageView.gestureRecognizers;
// 2. 移除
for (UIGestureRecognizer *ges in gesture) {
[self.imageView removeGestureRecognizer:ges];
}
// 根据选择的下标进行不同的操作,创建不同手势,触发不同手势的方法
switch (seg.selectedSegmentIndex) {
case 0:
{
// 创建点击手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 添加手势
[self.imageView addGestureRecognizer:tapGesture];
[tapGesture release];
}
break;
case 1:
{
// 创建长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.imageView addGestureRecognizer:longPress];
[longPress release];
}
break;
case 2:
{
// 创建清扫(滑动)手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 清扫手势只能有一个方向
swipe.direction = UISwipeGestureRecognizerDirectionDown;
[self.imageView addGestureRecognizer:swipe];
[swipe release];
}
break;
case 3:
{
// 创建捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.imageView addGestureRecognizer:pinch];
[pinch release];
}
break;
case 4:
{
// 创建旋转手势
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
[self.imageView addGestureRecognizer:rotate];
[rotate release];
}
break;
case 5:
{
// 创建拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.imageView addGestureRecognizer:pan];
[pan release];
}
break;
default:
break;
}
}
#pragma mark -
#pragma mark 点击手势事件
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"%s", __func__);
// 通过手势获得手势所在的视图
// UIImageView *aImage = (UIImageView *)tap.view;
// state是个枚举,根据手势不同状态操作不同的事情
if (tap.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始点击");
}
}
#pragma mark -
#pragma mark 旋转的手势事件
- (void)rotateAction:(UIRotationGestureRecognizer *)rotate
{
// 控制是否可以旋转的 rotation是角度的意思
// CGAffineTransformRotate C语言的函数 (角度基于视图最开始的位置,所以角度会越来越大)
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotate.rotation);
// 每次基于自身现在位置进行旋转 故角度为 0
rotate.rotation = 0;
NSLog(@"rotation = %f", rotate.rotation);
}
#pragma mark -
#pragma mark 长按的手势事件
- (void)longPressAction:(UILongPressGestureRecognizer *)press
{
if (press.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始");
}else if (press.state == UIGestureRecognizerStateEnded){
NSLog(@"结束");
}
}
#pragma mark -
#pragma mark 清扫的手势事件
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"shao");
}
#pragma mark -
#pragma mark 捏合的手势事件
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
// 后面俩个参数,是指放大比例,一般是等比例放大,即pinch.scale
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
// 每次对比例置 1
pinch.scale = 1;
NSLog(@"%g", pinch.velocity);
}
#pragma mark -
#pragma mark 拖拽的手势事件
- (void)panAction:(UIPanGestureRecognizer *)pan
{
// 获得当前视图的偏移量
CGPoint point = [pan translationInView:self.imageView];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
[pan setTranslation:CGPointMake(0, 0) inView:self.imageView];
}
效果图:
总结:
在view上添加tap(点击)手势,可相当于button的点击事件,同样的使用tap(点击)手势也可以写一个关灯游戏或者打地鼠.而这6个手势主要用于图像的操作,可以根据图片上不同的手势触发不同的方法来进行不同的操作.