`

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

    博客分类:
  • ios
 
阅读更多

首先新建一个基于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.  }  

 

分享到:
评论

相关推荐

    ios-手势控制:点击、滑动、平移、捏合、旋转、长按、轻扫.zip

    手势识别器(Gesture Recognizer)用于识别触摸序列并触发响应事件。当手势识别器识别到一个手势或手势发生变化时,会触发响应事件。UIGestureRecognizer类作为抽象类,不能直接使用。只能使用UIGestureRecognizer的...

    IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)

    下面通过图文并茂的方式给大家分享下IOS手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)的相关内容。 1、UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性。 iOS ...

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

    手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。 1、UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。 ...

    iOS开发之手势识别

    我们已经学习了触摸事件处理,但触摸事件处理起来很麻烦,每个触摸事件处理都需要实现3个touches方法,比较繁琐,实际上我们可以使用更加简单的触摸事件处理操作,那就是 手势识别UIGestureRecognizer 。 手势识别...

    iOS开发之手势gesture详解

    前言  在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,... UITapGestureRecognizer敲击手势(单击和双击) UIPanGestureReco

    IOS开发之手势响应事件优先级的实例详解

    交互响应事件都是通过手势的操作完成的,如点击、或双击、或长按,这些交互都是在视图中完成的,但是不同的视图可能会有不同的交互,有时候就会出现交互响应事件冲突的情况。这时候就需要处理事件优先级,以便达到想...

    ios关于手势

    UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势: 1、拍击UITapGestureRecognizer (任意...

    IOS 手势操作详解及实例总结篇

    iOS手势操作总结 手势操作种类 UITapGestureRecognizer: 敲击,点击 UILongPressGestureRecognizer: 长按 UIPinchGestureRecognizer: 缩放 UIRotationGestureRecognizer: 旋转 UISwipeGestureRecongizer: 轻扫 ...

    IOS中各种手势操作实例代码

    IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1、点击 UITapGestureRecognizer 2、平移 UIPanGestureRecognizer 3、缩放 UIPinchGestureRecognizer 4、旋转...

    IOS 开发之swift中手势的实例详解

    IOS 开发之swift中手势的实例详解 手势操作主要包括如下几类 手势 属性 说明 点击 UITapGestureRecognizer numberOfTapsRequired:点击的次数;numberOfTouchesRequired:点击时有手指数量 设置属性 ...

    开发绘图、手势综合App注意点

    手势的一些注意事项 对于 UITapGestureRecognizer 来说我们一般需要知道该点击手势在屏幕中的位置 (locationInView:self) 对于 UIPanGestureRecognizer 来说我们一般需要知道我们的滑动手势移动了多少距离 ...

    TVAnimationsGestures-Swift:这是一个简单的可展开可合并的表视图,用Swift语言编写

    UILongPressGestureRecognizer长按手势允许用户按住表视图的单元格来弹出一个发送邮件的小按钮以便用户将语录通过邮件与好友分享 主要文件 TableViewController.swift 表视图控制器,显示表演场次集合中的所有语录。...

    swift编写简单的左右菜单栏

    func tapGestureRecognized(tapGestureRecognizer : UITapGestureRecognizer){ self.didClose(); } // 菜单栏打开 func didOpen(){ var c = self.centerViewController.view.frame c.origin.x = ...

    iOS自定义水平滚动条、进度条

    简单说一下逻辑,新建一个继承UIView的类,分别给轨道、滑块添加UITapGestureRecognizer点击、UIPanGestureRecognizer滑动手势。获取偏移量,计算控件位置,刷新视图。 下面贴上核心代码: 显示视图,在控制器调用...

    ios滚动效果

    - (void)tapClick:(UITapGestureRecognizer *)tap { [UIView animateWithDuration:0.3 animations:^{ _lineView.frame = CGRectMake(tap.view.frame.origin.x, 47, tap.view.frame.size.width, 2); }]; //...

    简单易用的图片浏览器

    - (void)browseImage:(UITapGestureRecognizer *)tap { YLPhotoBrowser * browser = [[YLPhotoBrowser alloc] init]; browser.imageArray = _imgArray;//图片数组 browser.imageType = ImageType_Str;//图片类型 ...

    UIScrollView定时滚动和循环滚动,可点击图片和PageController

    UITapGestureRecognizer * gestd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [firstImageView addGestureRecognizer:gestd]; for (int i=0; i; i++) { ...

    DYERipple:创建一个漂亮的涟漪动画

    DYRipple:创建一个Ripple动画。 / * GIF上的动画速度较慢* /简单整合// :rect: Init the ...but you can expand this and use a different color if you likefunc singleTapView(tap: UITapGestureRecognizer) { //

    iOS实现手指点击出现波纹的效果

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)]; [self.view addGestureRecognizer:tap]; - (void)onTap:(UITapGestureRecognizer*)sender { ...

    IOS 解决UIButton 点击卡顿/延迟的问题

    前言  一开始还以为代码写的有问题,点击事件里面有... 解决办法:也没什么好办法,换成 ImageView 加 UITapGestureRecognizer 吧,另外奉上点击效果代码 :slightly_smiling_face: class UIImageViewEffect : UIIm

Global site tag (gtag.js) - Google Analytics