iOS——6种系统手势操作
UIGestureRecognizer(手勢(shì)識(shí)別器)
手勢(shì)識(shí)別在 iOS 中非常重要,他極大地提高了移動(dòng)設(shè)備的使用便捷性。
iOS 系統(tǒng)在 3.2 以后,他提供了六種常用的手勢(shì)(UIGestureRecognizer 的子類),開發(fā)者可以直接使用他們進(jìn)行手勢(shì)操作。
- UIPanGestureRecognizer(拖動(dòng))
- UIPinchGestureRecognizer(捏合)
- UIRotationGestureRecognizer(旋轉(zhuǎn))
- UITapGestureRecognizer(點(diǎn)按)
- UILongPressGestureRecognizer(長(zhǎng)按)
- ?UISwipeGestureRecognizer(輕掃)
另外,可以通過(guò)繼承 UIGestureRecognizer 類,實(shí)現(xiàn)自定義手勢(shì)(手勢(shì)識(shí)別器類)。
UIGestureRecognizer 的繼承關(guān)系:
手勢(shì)狀態(tài)
手勢(shì)狀態(tài)枚舉
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {UIGestureRecognizerStatePossible, // 尚未識(shí)別是何種手勢(shì)操作(但可能已經(jīng)觸發(fā)了觸摸事件),默認(rèn)狀態(tài)UIGestureRecognizerStateBegan, // 手勢(shì)已經(jīng)開始,此時(shí)已經(jīng)被識(shí)別,但是這個(gè)過(guò)程中可能發(fā)生變化,手勢(shì)操作尚未完成UIGestureRecognizerStateChanged, // 手勢(shì)狀態(tài)發(fā)生轉(zhuǎn)變UIGestureRecognizerStateEnded, // 手勢(shì)識(shí)別操作完成(此時(shí)已經(jīng)松開手指)UIGestureRecognizerStateCancelled, // 手勢(shì)被取消,恢復(fù)到默認(rèn)狀態(tài)UIGestureRecognizerStateFailed, // 手勢(shì)識(shí)別失敗,恢復(fù)到默認(rèn)狀態(tài)UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // 手勢(shì)識(shí)別完成,同UIGestureRecognizerStateEnded};在六種手勢(shì)識(shí)別中,只有一種手勢(shì)是離散型手勢(shì),就是 UITapGestureRecognizer(點(diǎn)按)。
離散型手勢(shì)的特點(diǎn):一旦識(shí)別就無(wú)法取消,而且只會(huì)調(diào)用一次手勢(shì)操作事件(初始化手勢(shì)時(shí)指定的回調(diào)方法)。
其他五種手勢(shì)都是連續(xù)型手勢(shì)。
連續(xù)型手勢(shì)的特點(diǎn):會(huì)多次調(diào)用手勢(shì)操作事件,而且在連續(xù)手勢(shì)識(shí)別后可以取消手勢(shì)。
從下圖可以看出兩者調(diào)用操作事件的次數(shù)是不同的:
對(duì)于離散型手勢(shì) UITapGestureRecgnizer 要么被識(shí)別,要么失敗,點(diǎn)按(假設(shè)點(diǎn)按次數(shù)設(shè)置為1,并且沒(méi)有添加長(zhǎng)按手勢(shì))下去一次不松開則此時(shí)什么也不會(huì)發(fā)生,松開手指立即識(shí)別并調(diào)用操作事件,并且狀態(tài)為3(已完成)。
但是連續(xù)型手勢(shì)要復(fù)雜一些,就拿旋轉(zhuǎn)手勢(shì)來(lái)說(shuō),如果兩個(gè)手指點(diǎn)下去不做任何操作,此時(shí)并不能識(shí)別手勢(shì)(因?yàn)槲覀冞€沒(méi)旋轉(zhuǎn))但是其實(shí)已經(jīng)觸發(fā)了觸摸開始事件,此時(shí)處于狀態(tài)0;如果此時(shí)旋轉(zhuǎn)會(huì)被識(shí)別,也就會(huì)調(diào)用對(duì)應(yīng)的操作事件,同時(shí)狀態(tài)變成1(手勢(shì)開始),但是狀態(tài)1只有一瞬間;緊接著狀態(tài)變?yōu)?(因?yàn)槲覀兊男D(zhuǎn)需要持續(xù)一會(huì)),并且重復(fù)調(diào)用操作事件(如果在事件中打印狀態(tài)會(huì)重復(fù)打印2);松開手指,此時(shí)狀態(tài)變?yōu)?,并調(diào)用1次操作事件。
連續(xù)手勢(shì)發(fā)生狀態(tài)轉(zhuǎn)換是由于觸摸事件中的移動(dòng)事件造成的,蘋果官方的分析圖也說(shuō)明了這一點(diǎn):
使用手勢(shì)步驟
使用手勢(shì)很簡(jiǎn)單,分為三步:
注意:一個(gè)手勢(shì)只能對(duì)應(yīng)一個(gè) View,但是一個(gè) View 可以有多個(gè)手勢(shì)。
1.點(diǎn)按手勢(shì)(UITapGestureRecognizer)
//創(chuàng)建手勢(shì)對(duì)象 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];//設(shè)置相關(guān)屬性 //點(diǎn)擊次數(shù)(默認(rèn)1) tap.numberOfTapsRequired = 1; //手指的個(gè)數(shù)(默認(rèn)1) tap.numberOfTouchesRequired = 1;//添加到視圖 [testView addGestureRecognizer:tap];關(guān)聯(lián)方法
- (void)tapClick:(UITapGestureRecognizer *)tap{NSLog(@"輕點(diǎn)手勢(shì)響應(yīng)!"); }2.長(zhǎng)按手勢(shì)(UILongPressGestureRecognizer)
//創(chuàng)建手勢(shì)對(duì)象 UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressClick:)];//設(shè)置相關(guān)屬性 //用幾個(gè)手指觸屏,默認(rèn)1 longPress.numberOfTouchesRequired = 1; //設(shè)置最短長(zhǎng)按時(shí)間,單位為秒(默認(rèn)0.5) longPress.minimumPressDuration = 1; //設(shè)置手勢(shì)識(shí)別期間所允許的手勢(shì)可移動(dòng)范圍 longPress.allowableMovement = 10;//添加到視圖 [testView addGestureRecognizer:longPress];關(guān)聯(lián)方法
- (void)longPressClick:(UILongPressGestureRecognizer *)press {//state屬性是所有手勢(shì)父類提供的方法,用于記錄手勢(shì)的狀態(tài)if (press.state == UIGestureRecognizerStateBegan) {NSLog(@"長(zhǎng)按手勢(shì)開始響應(yīng)!");} else if (press.state == UIGestureRecognizerStateChanged) {NSLog(@"長(zhǎng)按手勢(shì)狀態(tài)發(fā)生改變!");} else {NSLog(@"長(zhǎng)按手勢(shì)結(jié)束!");} }3.輕掃手勢(shì)(UISwipeGestureRecognizer)
//創(chuàng)建手勢(shì)對(duì)象(左掃) UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureClick:)];//設(shè)置相關(guān)屬性 //設(shè)置輕掃的方向 leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;//添加到視圖 [testView addGestureRecognizer:leftSwipe];//右掃 UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureClick:)];rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;[testView addGestureRecognizer:rightSwipe];關(guān)聯(lián)方法
-(void)swipeClick:(UISwipeGestureRecognizer *)swpie{//如果是左掃if (swpie.direction == UISwipeGestureRecognizerDirectionLeft ) {self.view.backgroundColor = [UIColor redColor];NSLog(@"左掃!");} else {self.view.backgroundColor = [UIColor greenColor];NSLog(@"右掃!");} }4.平移手勢(shì)(UIPanGestureRecognizer)
//創(chuàng)建手勢(shì)對(duì)象 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureClick:)];//添加到視圖 [testView addGestureRecognizer:pan];關(guān)聯(lián)方法
- (void)panClick:(UIPanGestureRecognizer *)pan {NSLog(@"響應(yīng)!!");//通過(guò)pan手勢(shì),能夠獲取到pan.view在self.view上的偏移量CGPoint point = [pan translationInView:self.view];NSLog(@"x=%.2lf y=%.2lf",point.x,point.y);//改變中心點(diǎn)坐標(biāo)(原來(lái)的中心點(diǎn)+偏移量=當(dāng)前的中心點(diǎn))CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);//CGPointZero<==>CGPointMake(0,0)//限制拖動(dòng)范圍newCenter.y = MAX(pan.view.frame.size.height/2, newCenter.y);newCenter.y = MIN(self.view.frame.size.height - pan.view.frame.size.height/2, newCenter.y);newCenter.x = MAX(pan.view.frame.size.width/2, newCenter.x);newCenter.x = MIN(self.view.frame.size.width - pan.view.frame.size.width/2, newCenter.x);pan.view.center = newCenter;//每次調(diào)用之后,需要重置手勢(shì)的偏移量,否則偏移量會(huì)自動(dòng)累加[pan setTranslation:CGPointZero inView:self.view]; }5.捏合手勢(shì)(UIPinchGestureRecognizer)
//創(chuàng)建手勢(shì)對(duì)象 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pichGestureClick:)];//添加到視圖 [imageView addGestureRecognizer:pinch];關(guān)聯(lián)方法
- (void)pichClick:(UIPinchGestureRecognizer *)pinch {//縮放的系數(shù)NSLog(@"%.2lf", pinch.scale);//固定寫法pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);//重置縮放系數(shù)(否則系數(shù)會(huì)累加)pinch.scale = 1.0; }6.旋轉(zhuǎn)手勢(shì)(UIRotationGestureRecognizer)
//創(chuàng)建手勢(shì)對(duì)象 UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureClick:)];//添加到視圖 [imageView addGestureRecognizer:rotation];關(guān)聯(lián)方法
- (void)rotationClick:(UIRotationGestureRecognizer *)rotation {//rotation.rotation 手勢(shì)旋轉(zhuǎn)的角度rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);//重置角度rotation.rotation = 0; }總結(jié)
以上是生活随笔為你收集整理的iOS——6种系统手势操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2073-三角形面积
- 下一篇: 大连医科大学中山学院计算机科学与技术,大