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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容