iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
首先新建一個(gè)基于Sigle view Application的項(xiàng)目,名為GestureTest;我的項(xiàng)目結(jié)構(gòu)如下:
往viewController.xib文件里拖動(dòng)一個(gè)imageView,并使覆蓋整個(gè)屏幕,改動(dòng)屬性為:
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文件的實(shí)現(xiàn)部分:
?
[cpp]viewplaincopy
?
1.?????@synthesize?imageView;??
2.???????
3.?????CGFloat?lastScaleFactor=1;//放大、縮小??
4.?????CGFloat??netRotation;//旋轉(zhuǎn)??
5.?????CGPoint?netTranslation;//平衡??
6.?????NSArray?*images;//圖片數(shù)組??
7.?????int?imageIndex=0;//數(shù)組下標(biāo)??
8.???????
9.?????-?(void)viewDidLoad??
10.???{??
11.???????//1、創(chuàng)建手勢(shì)實(shí)例,并連接方法handleTapGesture,點(diǎn)擊手勢(shì)??
12.???????UITapGestureRecognizer?*tapGesture=[[UITapGestureRecognizer?alloc]initWithTarget:self?action:@selector(handleTapGesture:)];??
13.???????//設(shè)置手勢(shì)點(diǎn)擊數(shù),雙擊:點(diǎn)2下??
14.???????tapGesture.numberOfTapsRequired=2;??
15.???????//?imageView添加手勢(shì)識(shí)別??
16.???????[imageView?addGestureRecognizer:tapGesture];??
17.???????//釋放內(nèi)存??
18.???????[tapGesture?release];??
19.?????????
20.???????//2、手勢(shì)為捏的姿勢(shì):按住option按鈕配合鼠標(biāo)來(lái)做這個(gè)動(dòng)作在虛擬器上??
21.???????UIPinchGestureRecognizer?*pinchGesture=[[UIPinchGestureRecognizer?alloc]initWithTarget:self?action:@selector(handlePinchGesture:)];??
22.???????[imageView?addGestureRecognizer:pinchGesture];//imageView添加手勢(shì)識(shí)別??
23.???????[pinchGesture?release];??
24.?????????
25.???????//3、旋轉(zhuǎn)手勢(shì):按住option按鈕配合鼠標(biāo)來(lái)做這個(gè)動(dòng)作在虛擬器上??
26.???????UIRotationGestureRecognizer?*rotateGesture=[[UIRotationGestureRecognizer?alloc]initWithTarget:self?action:@selector(handleRotateGesture:)];??
27.???????[imageView?addGestureRecognizer:rotateGesture];??
28.???????[rotateGesture?release];??
29.?????????
30.???????//4、拖手勢(shì)??
31.???????UIPanGestureRecognizer?*panGesture=[[UIPanGestureRecognizer?alloc]initWithTarget:self?action:@selector(handlePanGesture:)];??
32.??????//?[imageView?addGestureRecognizer:panGesture];??
33.???????[panGesture?release];??
34.?????????
35.???????//5、劃動(dòng)手勢(shì)??
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;//不設(shè)置黑夜是右??
44.???????[imageView?addGestureRecognizer:swipeLeftGesture];??
45.???????[swipeLeftGesture?release];??
46.?????????
47.???????//6、長(zhǎng)按手勢(shì)??
48.???????UILongPressGestureRecognizer?*longpressGesutre=[[UILongPressGestureRecognizer?alloc]initWithTarget:self?action:@selector(handleLongpressGesture:)];??
49.???????//長(zhǎng)按時(shí)間為1秒??
50.???????longpressGesutre.minimumPressDuration=1;??
51.???????//允許15秒中運(yùn)動(dòng)??
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.???//雙擊屏幕時(shí)會(huì)調(diào)用此方法,放大和縮小圖片??
62.???-(IBAction)handleTapGesture:(UIGestureRecognizer*)sender{??
63.???????//判斷imageView的內(nèi)容模式是否是UIViewContentModeScaleAspectFit,該模式是原比例,按照?qǐng)D片原時(shí)比例顯示大小??
64.???????if(sender.view.contentMode==UIViewContentModeScaleAspectFit){??
65.???????????//把imageView模式改成UIViewContentModeCenter,按照?qǐng)D片原先的大小顯示中心的一部分在imageView??
66.???????????sender.view.contentMode=UIViewContentModeCenter;??
67.???????}else{??
68.???????????sender.view.contentMode=UIViewContentModeScaleAspectFit;??
69.???????}??
70.???}??
71.???//捏的手勢(shì),使圖片放大和縮小,捏的動(dòng)作是一個(gè)連續(xù)的動(dòng)作??
72.???-(IBAction)handlePinchGesture:(UIGestureRecognizer*)sender{??
73.???????//得到sender捏手勢(shì)的大小??
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.???????//狀態(tài)是否結(jié)束,如果結(jié)束保存數(shù)據(jù)??
85.???????if(sender.state==UIGestureRecognizerStateEnded){??
86.???????????if(factor>1){??
87.???????????????lastScaleFactor+=(factor-1);??
88.???????????}else{??
89.???????????????lastScaleFactor*=factor;??
90.???????????}??
91.???????}??
92.???}??
93.???//旋轉(zhuǎn)手勢(shì)??
94.???-(IBAction)handleRotateGesture:(UIGestureRecognizer*)sender{??
95.???????//浮點(diǎn)類型,得到sender的旋轉(zhuǎn)度數(shù)??
96.???????CGFloat?rotation=[(UIRotationGestureRecognizer*)sender?rotation];??
97.???????//旋轉(zhuǎn)角度CGAffineTransformMakeRotation??
98.???????CGAffineTransform?transform=CGAffineTransformMakeRotation(rotation+netRotation);??
99.???????//改變圖像角度??
100.? ????sender.view.transform=transform;??
101.? ????//狀態(tài)結(jié)束,保存數(shù)據(jù)??
102.? ????if(sender.state==UIGestureRecognizerStateEnded){??
103.? ????????netRotation+=rotation;??
104.? ????}??
105.? ???????
106.? }??
107.? //拖手勢(shì)??
108.? -(IBAction)handlePanGesture:(UIGestureRecognizer*)sender{??
109.? ????//得到拖的過(guò)程中的xy坐標(biāo)??
110.? ????CGPoint?translation=[(UIPanGestureRecognizer*)sender?translationInView:imageView];??
111.? ????//平移圖片CGAffineTransformMakeTranslation??
112.? ????sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x,?netTranslation.y+translation.y);??
113.? ????//狀態(tài)結(jié)束,保存數(shù)據(jù)??
114.? ????if(sender.state==UIGestureRecognizerStateEnded){??
115.? ????????netTranslation.x+=translation.x;??
116.? ????????netTranslation.y+=translation.y;??
117.? ????}??
118.? ??????
119.? }??
120.? //劃動(dòng)手勢(shì)??
121.? -(IBAction)handleSwipeGesture:(UIGestureRecognizer*)sender{??
122.? ????//劃動(dòng)的方向??
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++;//下標(biāo)++??
135.? ????????????break;??
136.? ????????case?UISwipeGestureRecognizerDirectionRight:??
137.? ????????????NSLog(@"right");??
138.? ????????????imageIndex--;//下標(biāo)--??
139.? ????????????break;??
140.? ????????default:??
141.? ????????????break;??
142.? ????}??
143.? ????//得到不越界不<0的下標(biāo)??
144.? ????imageIndex=(imageIndex<0)?([images?count]-1):imageIndex%[images?count];??
145.? ????//imageView顯示圖片??
146.? ????imageView.image=[UIImage?imageNamed:[images?objectAtIndex:imageIndex]];??
147.? ??????
148.? }??
149.? //長(zhǎng)按手勢(shì)??
150.? -(IBAction)handleLongpressGesture:(UIGestureRecognizer*)sender{??
151.? ????//創(chuàng)建警告??
152.? ????UIActionSheet?*actionSheet=[[UIActionSheet?alloc]initWithTitle:@"Image?options"?delegate:self?cancelButtonTitle:nil?destructiveButtonTitle:nil?otherButtonTitles:@"Save?Image",@"Copy",?nil];??
153.? ????//當(dāng)前view顯示警告??
154.? ????[actionSheet?showInView:self.view];??
155.? ????[actionSheet?release];??
156.? }??
157.? -(void)dealloc{??
158.? ????[images?release];??
159.? ????[imageView?release];??
160.? ????[super?dealloc];??
161.? } ?
總結(jié)
以上是生活随笔為你收集整理的iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 建筑业房地产管理系统
- 下一篇: Maven Helper 插件介绍