ios中主要的手勢(shì)

事件響應(yīng)鏈的傳播路線:

initial view –> super view –> …..–> view controller –> window –> Application –> AppDelegate

UIResponse的觸碰方法:

/**** 觸摸事件方法拷况,對(duì)觸摸事件進(jìn)行處理 *****/-(void)touchesBegan:(NSSet*)touches withEvent:(nullableUIEvent*)event;//觸摸開始-(void)touchesMoved:(NSSet*)touches withEvent:(nullableUIEvent*)event;//觸摸移動(dòng)-(void)touchesEnded:(NSSet*)touches withEvent:(nullableUIEvent*)event;//觸摸結(jié)束-(void)touchesCancelled:(nullableNSSet*)touches withEvent:(nullableUIEvent*)event;//觸摸取消

ios中主要的手勢(shì):

1.UITapGestureRecognizer //單擊手勢(shì)

?點(diǎn)擊作為最常用手勢(shì),用于按下或選擇一個(gè)控件或條目(類似于普通的鼠標(biāo)點(diǎn)擊)

/**

添加點(diǎn)擊手勢(shì)

*/-(void)addTapGestureWithTarget:(id)sender{//1,tap(點(diǎn)擊)UITapGestureRecognizer*tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(tapGestureAction:)];//手勢(shì)點(diǎn)擊次數(shù)tapGesture.numberOfTapsRequired=1;// Default is 1//點(diǎn)擊手指數(shù)量tapGesture.numberOfTouchesRequired=1;// Default is 1//將手勢(shì)識(shí)別器添加到view上[sender addGestureRecognizer:tapGesture];}/**

點(diǎn)擊手勢(shì)的響應(yīng)方法

*/-(void)tapGestureAction:(UITapGestureRecognizer*)gesture{NSLog(@"%s",__FUNCTION__);UILabel*tempLabel=[self.view? viewWithTag:1001];NSInteger tapCount=gesture.numberOfTapsRequired;//點(diǎn)擊次數(shù)NSInteger touchCount=gesture.numberOfTouchesRequired;//手指個(gè)數(shù)tempLabel.text=[NSString stringWithFormat:@"手指個(gè)數(shù):%ld? 點(diǎn)擊次數(shù):%ld",(long)touchCount,(long)tapCount];}


2.UIPanGestureRecognizer //拖動(dòng)手勢(shì)

拖動(dòng)用于實(shí)現(xiàn)一些頁面的滾動(dòng)垂蜗,以及對(duì)控件的移動(dòng)功能凄鼻。

/**

添加拖拽手勢(shì)

*/-(void)addPanGestureWithView:(id)sender{//2,pan(平移)UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:selfaction:@selector(panGestureAction:)];panGesture.minimumNumberOfTouches=1;//最少點(diǎn)擊次數(shù)---[sender addGestureRecognizer:panGesture];}/**

拖拽手勢(shì)相應(yīng)方法

*/-(void)panGestureAction:(UIPanGestureRecognizer*)gesture{switch(gesture.state){//開始caseUIGestureRecognizerStateBegan:{NSLog(@"開始");}break;//改變caseUIGestureRecognizerStateChanged:{//1,改變r(jià)edView的frameUILabel*redView=(UILabel*)gesture.view;//2,改變的坐標(biāo)-移動(dòng)的距離CGPoint point=[gesture translationInView:redView];NSLog(@"%@",NSStringFromCGPoint(point));//3,根據(jù)移動(dòng)的距離改變r(jià)edView的frame//CGRectOffset - 根據(jù)偏移量改變view的x值和y值redView.frame=CGRectOffset(redView.frame,point.x,point.y);//清空偏移量的累加值[gesture setTranslation:CGPointZero inView:redView];}break;//結(jié)束caseUIGestureRecognizerStateEnded:{NSLog(@"結(jié)束");}break;default:break;}NSLog(@"%s",__FUNCTION__);}


3.UISwipeGestureRecognizer //輕掃手勢(shì)

橫掃手勢(shì)用于激活列表項(xiàng)的快捷操作菜單

/**

添加輕掃手勢(shì)

*/-(void)addSwipeGestureWithView:(UIView*)aView{//3,swipe(輕掃)UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(swipeGestureAction:)];//8個(gè)方向//direction - 方向/*

? ? typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {

? ? UISwipeGestureRecognizerDirectionRight = 1 << 0,

? ? UISwipeGestureRecognizerDirectionLeft? = 1 << 1,

? ? UISwipeGestureRecognizerDirectionUp? ? = 1 << 2,

? ? UISwipeGestureRecognizerDirectionDown? = 1 << 3

? ? };

? ? UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionUp = 6

? ? UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionDown = 9

? ? UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionDown = 10

? ? UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionUp = 5

? ? */swipeGesture.direction=UISwipeGestureRecognizerDirectionRight;[aView addGestureRecognizer:swipeGesture];}/**

輕掃手勢(shì)響應(yīng)方法

*/-(void)swipeGestureAction:(UISwipeGestureRecognizer*)gesture{NSLog(@"%s",__FUNCTION__);intdirection=gesture.direction;UILabel*tempLabel=(UILabel*)gesture.view;if(direction==1){tempLabel.text=[NSString stringWithFormat:@"滑動(dòng)方向:右"];}elseif(direction==2){tempLabel.text=[NSString stringWithFormat:@"滑動(dòng)方向:左"];}elseif(direction==3){tempLabel.text=[NSString stringWithFormat:@"滑動(dòng)方向:上"];}elseif(direction==4){tempLabel.text=[NSString stringWithFormat:@"滑動(dòng)方向:下"];}}


4.UIPinchGestureRecognizer //捏合手勢(shì)

即放大縮小


/**

添加捏合手勢(shì)

*/-(void)addPinchGestureWithView:(UIView*)aView{//4,pinch捏和UIPinchGestureRecognizer*pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:selfaction:@selector(pinchGestureAction:)];[self.pinchImg addGestureRecognizer:pinchGesture];}/**

捏合手勢(shì)響應(yīng)方法

*/-(void)pinchGestureAction:(UIPinchGestureRecognizer*)gesture{switch(gesture.state){caseUIGestureRecognizerStateBegan:{//手勢(shì)開始//記錄,當(dāng)前view的frame,作為原始frameredViewRect=gesture.view.frame;}break;caseUIGestureRecognizerStateChanged:{//1,拿到viewUIView*redView=gesture.view;//2,改變view的frame//scale 縮放后的比例,跟1(原來的frame)CGFloat dx=CGRectGetWidth(redViewRect)*(1-gesture.scale);//寬度變化量CGFloat dy=CGRectGetHeight(redViewRect)*(1-gesture.scale);//高度變化量//dx dy 縮放的偏移量redView.frame=CGRectInset(redViewRect,dx,dy);}break;caseUIGestureRecognizerStateEnded:{}break;default:break;}}


5.UIScreenEdgePanGestureRecognizer//邊緣滑入

滑動(dòng)用于實(shí)現(xiàn)頁面的快速滾動(dòng)和翻頁的功能。


/**

添加邊緣滑入手勢(shì)

*/-(void)addScreenEdgePanGestureWithView:(UIView*)aView{//5,ScreenEdgePan邊緣劃入U(xiǎn)IScreenEdgePanGestureRecognizer*sePanGesture=[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:selfaction:@selector(seGestureAction:)];//劃入的位置-(邊緣的位置)/*

? ? typedef NS_OPTIONS(NSUInteger, UIRectEdge) {

? ? ? ? UIRectEdgeNone? = 0,

? ? ? ? UIRectEdgeTop? ? = 1 << 0,

? ? ? ? UIRectEdgeLeft? = 1 << 1,

? ? ? ? UIRectEdgeBottom = 1 << 2,

? ? ? ? UIRectEdgeRight? = 1 << 3,

? ? ? ? UIRectEdgeAll? ? = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight

? ? }*/sePanGesture.edges=UIRectEdgeLeft;[aView addGestureRecognizer:sePanGesture];}/**

邊緣劃入響應(yīng)方法

*/-(void)seGestureAction:(UIScreenEdgePanGestureRecognizer*)gesture{NSLog(@"%s",__FUNCTION__);UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"邊緣滑入"message:nil preferredStyle:UIAlertControllerStyleAlert];UIAlertAction*action=[UIAlertAction actionWithTitle:@"確定"style:UIAlertActionStyleDestructive handler:nil];[alert addAction:action];[selfpresentViewController:alert animated:YES completion:nil];}


6.UIRotationGestureRecognizer //旋轉(zhuǎn)手勢(shì)

即將視圖圍繞中心點(diǎn)轉(zhuǎn)動(dòng)

/**

添加旋轉(zhuǎn)手勢(shì)

*/-(void)addRotationGestureWithView:(UIView*)aView{//6,rotation旋轉(zhuǎn)UIRotationGestureRecognizer*rotationGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:selfaction:@selector(rotationGestureAction:)];[aView addGestureRecognizer:rotationGesture];}/**

旋轉(zhuǎn)手勢(shì)響應(yīng)方法

*/-(void)rotationGestureAction:(UIRotationGestureRecognizer*)gesture{CGFloat? rotation=gesture.rotation;//旋轉(zhuǎn)的弧度CGFloat velocity=gesture.velocity;//旋轉(zhuǎn)速度 (radians/second)gesture.view.transform=CGAffineTransformMakeRotation(rotation);UILabel*tempLabel=(UILabel*)gesture.view;tempLabel.text=[NSString stringWithFormat:@"旋轉(zhuǎn)弧度:%f \n 旋轉(zhuǎn)速度:%f",rotation,velocity];}


7.UILongPressGestureRecognizer //長按手勢(shì)

將出現(xiàn)編輯菜單

/**

添加長按手勢(shì)

*/-(void)addLongPressGestureWithView:(UIView*)aView{//7,longPress長按UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:selfaction:@selector(longPressAction:)];/* numberOfTouchesRequired這個(gè)屬性保存了有多少個(gè)手指點(diǎn)擊了屏幕,因此你要確保你每次的點(diǎn)擊手指數(shù)目是一樣的,默認(rèn)值是為 0. */longPress.numberOfTouchesRequired=1;//手指個(gè)數(shù)//longPress.minimumPressDuration = 2;//按的最少時(shí)長/*最大100像素的運(yùn)動(dòng)是手勢(shì)識(shí)別所允許的? Default is 10.*/longPress.allowableMovement=100;///*這個(gè)參數(shù)表示,兩次點(diǎn)擊之間間隔的時(shí)間長度职辅。Default is 0.5.*/longPress.minimumPressDuration=1.0;[aView addGestureRecognizer:longPress];}/**

長按相應(yīng)方法

*/-(void)longPressAction:(UILongPressGestureRecognizer*)gesture{NSLog(@"%s",__FUNCTION__);//彈出menuswitch(gesture.state){caseUIGestureRecognizerStateBegan:{//UIMenuController menu控制器//系統(tǒng)的粘貼復(fù)制的小彈框//menuController? 單例UIMenuController*ctr=[UIMenuController sharedMenuController];/*自定義Menu按鈕

? ? ? ? ? ? //menu按鈕

? ? ? ? ? ? UIMenuItem *mItem = [[UIMenuItem alloc]initWithTitle:@"自定義" action:@selector(longPressMenuAction)];


? ? ? ? ? ? UIMenuItem *mItem1 = [[UIMenuItem alloc]initWithTitle:@"復(fù)制" action:@selector(longPressMenuAction)];

? ? ? ? ? ? UIMenuItem *mItem2 = [[UIMenuItem alloc]initWithTitle:@"粘貼" action:@selector(longPressMenuAction)];


? ? ? ? ? ? //將item添加到controller中

? ? ? ? ? ? ctr.menuItems = [NSArray? arrayWithObjects:mItem,mItem1,mItem2,nil];//@[mItem,mItem1,mItem2];

? ? ? ? ? ? *///獲得手指點(diǎn)擊的位置CGPoint point=[gesture locationInView:gesture.view];//設(shè)置顯示的位置[ctr setTargetRect:CGRectMake(point.x,point.y,0,0)inView:gesture.view];//顯示menu工具條[ctr setMenuVisible:YES animated:YES];}break;default:break;}}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末刨裆,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子它掂,更是在濱河造成了極大的恐慌巴帮,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件群发,死亡現(xiàn)場離奇詭異晰韵,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)熟妓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門雪猪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人起愈,你說我怎么就攤上這事只恨。” “怎么了抬虽?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵官觅,是天一觀的道長。 經(jīng)常有香客問我阐污,道長休涤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮功氨,結(jié)果婚禮上序苏,老公的妹妹穿的比我還像新娘。我一直安慰自己捷凄,他們只是感情好忱详,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著跺涤,像睡著了一般匈睁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上桶错,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天航唆,我揣著相機(jī)與錄音,去河邊找鬼牛曹。 笑死佛点,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的黎比。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼鸳玩,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼阅虫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起不跟,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤颓帝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后窝革,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體购城,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年虐译,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了瘪板。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡漆诽,死狀恐怖侮攀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情厢拭,我是刑警寧澤兰英,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站供鸠,受9級(jí)特大地震影響畦贸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜楞捂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一薄坏、第九天 我趴在偏房一處隱蔽的房頂上張望趋厉。 院中可真熱鬧,春花似錦颤殴、人聲如沸觅廓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杈绸。三九已至,卻和暖如春矮瘟,著一層夾襖步出監(jiān)牢的瞬間瞳脓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工澈侠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留劫侧,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓哨啃,卻偏偏與公主長得像烧栋,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拳球,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359