資源描述:
《ios手勢識(shí)別地詳細(xì)使用》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、實(shí)用標(biāo)準(zhǔn)文案iOS手勢識(shí)別的詳細(xì)使用(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢依賴,自定義手勢)1、UIGestureRecognizer介紹手勢識(shí)別在iOS上非常重要,手勢操作移動(dòng)設(shè)備的重要特征,極大的增加了移動(dòng)設(shè)備使用便捷性。iOS系統(tǒng)在3.2以后,為方便開發(fā)這使用一些常用的手勢,提供了UIGestureRecognizer類。手勢識(shí)別UIGestureRecognizer類是個(gè)抽象類,下面的子類是具體的手勢,開發(fā)這可以直接使用這些手勢識(shí)別。·UITapGestureRecognizer??·UIPinchGestureRecognizer·UIRotationGestureRecogni
2、zer·UISwipeGestureRecognizer·UIPanGestureRecognizer·UILongPressGestureRecognizer上面的手勢對應(yīng)的操作是:?·Tap(點(diǎn)一下)·Pinch(二指往內(nèi)或往外撥動(dòng),平時(shí)經(jīng)常用到的縮放)·Rotation(旋轉(zhuǎn))·Swipe(滑動(dòng),快速移動(dòng))·Pan(拖移,慢速移動(dòng))·?LongPress(長按)UIGestureRecognizer的繼承關(guān)系如下:精彩文檔實(shí)用標(biāo)準(zhǔn)文案2、使用手勢的步驟使用手勢很簡單,分為兩步:1.創(chuàng)建手勢實(shí)例。當(dāng)創(chuàng)建手勢時(shí),指定一個(gè)回調(diào)方法,當(dāng)手勢開始,改變、或結(jié)束時(shí),回調(diào)方法被調(diào)用。2.添
3、加到需要識(shí)別的View中。每個(gè)手勢只對應(yīng)一個(gè)View,當(dāng)屏幕觸摸在View的邊界內(nèi)時(shí),如果手勢和預(yù)定的一樣,那就會(huì)回調(diào)方法。ps:一個(gè)手勢只能對應(yīng)一個(gè)View,但是一個(gè)View可以有多個(gè)手勢。建議在真機(jī)上運(yùn)行這些手勢,模擬器操作不太方便,可能導(dǎo)致你認(rèn)為手勢失效。3、Pan拖動(dòng)手勢:[cpp]?viewplaincopy1.UIImageView?*snakeImageView?=?[[UIImageView?alloc]?initWithImage:[UIImage?imageNamed:@"snake.png"]];??2.snakeImageView.frame?=?CGRec
4、tMake(50,?50,?100,?160);??3.UIPanGestureRecognizer?*panGestureRecognizer?=?[[UIPanGestureRecognizer?alloc]??4.????????????????????????????????????????????????initWithTarget:self??5.????????????????????????????????????????????????action:@selector(handlePan:)];??????6.[snakeImageView?addGestureR
5、ecognizer:panGestureRecognizer];??7.[self.view?setBackgroundColor:[UIColor?whiteColor]];??8.[self.view?addSubview:snakeImageView];??精彩文檔實(shí)用標(biāo)準(zhǔn)文案新建一個(gè)ImageView,然后添加手勢回調(diào)方法:[cpp]?viewplaincopy1.-?(void)?handlePan:(UIPanGestureRecognizer*)?recognizer??2.{??3.????CGPoint?translation?=?[recognizer?tran
6、slationInView:self.view];??4.????recognizer.view.center?=?CGPointMake(recognizer.view.center.x?+?translation.x,??5.???????????????????????????????????recognizer.view.center.y?+?translation.y);??6.????[recognizer?setTranslation:CGPointZero?inView:self.view];??7.??????8.}??4、Pinch縮放手勢[cpp]?viewp
7、laincopy1.UIPinchGestureRecognizer?*pinchGestureRecognizer?=?[[UIPinchGestureRecognizer?alloc]??2.????????????????????????????????????????????????????????initWithTarget:self??3.????????????????????????????????????????????????????????act