首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下:

往viewController.xib文件里拖动一个imageView,并使覆盖整个屏幕,改动属性为:

viewController.h文件:

[cpp]viewplaincopy

1.     #import <UIKit/UIKit.h>

2.

3.     @interface ViewController : UIViewController{

4.         IBOutlet UIImageView *imageView;

5.     }

6.     @property (nonatomic,retain)IBOutlet UIImageView *imageView;

7.     @end

并使xib文件里的imageView与之连接;

然后是viewController.m文件的实现部分:

[cpp]viewplaincopy

1.     @synthesize imageView;

2.

3.     CGFloat lastScaleFactor=1;//放大、缩小

4.     CGFloat  netRotation;//旋转

5.     CGPoint netTranslation;//平衡

6.     NSArray *images;//图片数组

7.     int imageIndex=0;//数组下标

8.

9.     - (void)viewDidLoad

10.   {

11.       //1、创建手势实例,并连接方法handleTapGesture,点击手势

12.       UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];

13.       //设置手势点击数,双击:点2下

14.       tapGesture.numberOfTapsRequired=2;

15.       // imageView添加手势识别

16.       [imageView addGestureRecognizer:tapGesture];

17.       //释放内存

18.       [tapGesture release];

19.

20.       //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上

21.       UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];

22.       [imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别

23.       [pinchGesture release];

24.

25.       //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上

26.       UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];

27.       [imageView addGestureRecognizer:rotateGesture];

28.       [rotateGesture release];

29.

30.       //4、拖手势

31.       UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];

32.      // [imageView addGestureRecognizer:panGesture];

33.       [panGesture release];

34.

35.       //5、划动手势

36.       images=[[NSArray alloc]initWithObjects:@"cell.jpg",@"heihua.jpg",@"xuanyi.jpg", nil];

37.       //右划

38.       UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];

39.       [imageView addGestureRecognizer:swipeGesture];

40.       [swipeGesture release];

41.       //左划

42.       UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];

43.       swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右

44.       [imageView addGestureRecognizer:swipeLeftGesture];

45.       [swipeLeftGesture release];

46.

47.       //6、长按手势

48.       UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];

49.       //长按时间为1秒

50.       longpressGesutre.minimumPressDuration=1;

51.       //允许15秒中运动

52.       longpressGesutre.allowableMovement=15;

53.       //所需触摸1次

54.       longpressGesutre.numberOfTouchesRequired=1;

55.       [imageView addGestureRecognizer:longpressGesutre];

56.       [longpressGesutre release];

57.

58.       [super viewDidLoad];

59.       // Do any additional setup after loading the view, typically from a nib.

60.   }

61.   //双击屏幕时会调用此方法,放大和缩小图片

62.   -(IBAction)handleTapGesture:(UIGestureRecognizer*)sender{

63.       //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小

64.       if(sender.view.contentMode==UIViewContentModeScaleAspectFit){

65.           //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView

66.           sender.view.contentMode=UIViewContentModeCenter;

67.       }else{

68.           sender.view.contentMode=UIViewContentModeScaleAspectFit;

69.       }

70.   }

71.   //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作

72.   -(IBAction)handlePinchGesture:(UIGestureRecognizer*)sender{

73.       //得到sender捏手势的大小

74.       CGFloat factor=[(UIPinchGestureRecognizer*)sender scale];

75.       if(factor>1){

76.           //图片放大

77.           sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor+(factor-1), (lastScaleFactor+(factor-1)));

78.

79.       }else{

80.           //缩小

81.           sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor);

82.

83.       }

84.       //状态是否结束,如果结束保存数据

85.       if(sender.state==UIGestureRecognizerStateEnded){

86.           if(factor>1){

87.               lastScaleFactor+=(factor-1);

88.           }else{

89.               lastScaleFactor*=factor;

90.           }

91.       }

92.   }

93.   //旋转手势

94.   -(IBAction)handleRotateGesture:(UIGestureRecognizer*)sender{

95.       //浮点类型,得到sender的旋转度数

96.       CGFloat rotation=[(UIRotationGestureRecognizer*)sender rotation];

97.       //旋转角度CGAffineTransformMakeRotation

98.       CGAffineTransform transform=CGAffineTransformMakeRotation(rotation+netRotation);

99.       //改变图像角度

100.      sender.view.transform=transform;

101.      //状态结束,保存数据

102.      if(sender.state==UIGestureRecognizerStateEnded){

103.          netRotation+=rotation;

104.      }

105.

106.  }

107.  //拖手势

108.  -(IBAction)handlePanGesture:(UIGestureRecognizer*)sender{

109.      //得到拖的过程中的xy坐标

110.      CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:imageView];

111.      //平移图片CGAffineTransformMakeTranslation

112.      sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y);

113.      //状态结束,保存数据

114.      if(sender.state==UIGestureRecognizerStateEnded){

115.          netTranslation.x+=translation.x;

116.          netTranslation.y+=translation.y;

117.      }

118.

119.  }

120.  //划动手势

121.  -(IBAction)handleSwipeGesture:(UIGestureRecognizer*)sender{

122.      //划动的方向

123.      UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer*) sender direction];

124.      //判断是上下左右

125.      switch (direction) {

126.          case UISwipeGestureRecognizerDirectionUp:

127.              NSLog(@"up");

128.              break;

129.          case UISwipeGestureRecognizerDirectionDown:

130.              NSLog(@"down");

131.              break;

132.          case UISwipeGestureRecognizerDirectionLeft:

133.              NSLog(@"left");

134.              imageIndex++;//下标++

135.              break;

136.          case UISwipeGestureRecognizerDirectionRight:

137.              NSLog(@"right");

138.              imageIndex--;//下标--

139.              break;

140.          default:

141.              break;

142.      }

143.      //得到不越界不<0的下标

144.      imageIndex=(imageIndex<0)?([images count]-1):imageIndex%[images count];

145.      //imageView显示图片

146.      imageView.image=[UIImage imageNamed:[images objectAtIndex:imageIndex]];

147.

148.  }

149.  //长按手势

150.  -(IBAction)handleLongpressGesture:(UIGestureRecognizer*)sender{

151.      //创建警告

152.      UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"Image options" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save Image",@"Copy", nil];

153.      //当前view显示警告

154.      [actionSheet showInView:self.view];

155.      [actionSheet release];

156.  }

157.  -(void)dealloc{

158.      [images release];

159.      [imageView release];

160.      [super dealloc];

161.  }

转:http://blog.csdn.net/chang6520/article/details/7924313

iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer的更多相关文章

  1. iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer

    首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...

  2. Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)

    // //  ViewController.swift //  JieUITapGestureRecognizer // //  Created by jiezhang on 14-10-4. //  ...

  3. Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍

    1.UITapGestureRecognizer 点击/双击手势 代码如下: var tapGesture = UITapGestureRecognizer(target: self, action: ...

  4. iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)       1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...

  5. ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)

    iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...

  6. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  7. 利用javascript实现在圆周上匀速划动的动画效果

    先看下效果:          

  8. android提供ToolBar实现划动菜单的陷阱

    代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android: ...

  9. ios7禁止默认划动返回

    self.navigationController.interactivePopGestureRecognizer.enabled = NO; 或 在使用之前先要判断是否ios7,不然会导致crash ...

随机推荐

  1. Laravel Model 的 fillable (白名单)与 guarded (黑名单)

    例如 protected $fillable = ['name']; protected $guarded = ['price']; 定义了 name 字段可以写入/修改,而 price 字段不可以. ...

  2. 2018-2019-2 网络对抗技术 20165301 Exp4 恶意代码分析

    2018-2019-2 网络对抗技术 20165301 Exp4 恶意代码分析 实验内容 系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是哪里.运行一段时间并分 ...

  3. 移动端console.log()调试

    在微信或app进行开发的时候,没法直接查看console.log的输出内容,调试起来简直太痛苦了. 1.笨笨的方法 fiddler抓请求:追加dom节点,显示调试信息. var div =docume ...

  4. Ubuntu出现apt-get: Package has no installation candidate问题

    今天在安装 vim 的时候出现了 Package 'vim' has no installation candidate的问题 解决方法如下:# apt-get update# apt-get upg ...

  5. .NetCore Linux环境下安装InfluxDB以及配置设置

    Linux下安装 确定需要安装的版本,我的linux是干净的,所以我需要先安装wget yum -y install wget 下载安装 wget https://dl.influxdata.com/ ...

  6. linux 检测远程端口是否打开

    linux 检测远程端口是否打开   检测远程端口是否打开   常用telnet 110.101.101.101 80方式测试远程主机端口是否打开.   除此之外还可以使用:   方法1.nmap i ...

  7. mybatis的快速入门

    说明: 在这个部分,会写个简单的入门案例. 然后,会重新写一个,更加严格的程序案例. 一:案例一 1.最终的目录结构 2.新建一个普通的Java项目,并新建lib 在项目名上右键,不是src. 3.导 ...

  8. SpringBoot的Controller使用

    一: 1.注解 2.control注解 3.效果 4.RespomseBody package com.caojun.springboot; import org.springframework.be ...

  9. jquery获取浏览器各种高宽

    $(document).ready(function(){ alert($(window).height()); //浏览器当前窗口可视区域高度 alert($(document).height()) ...

  10. Mysql Lock wait timeout exceeded; try restarting transaction的问题

    今天在后台跑任务的时候,发现了数据库报错1205 - Lock wait timeout exceeded; try restarting transaction.问题原因是因为表的事务锁,以下是解决 ...