美文网首页
ios中主要的手势

ios中主要的手势

作者: c2ebc00a0e48 | 来源:发表于2020-06-11 11:18 被阅读0次

    事件响应链的传播路线:

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

    UIResponse的触碰方法:

    /**** 触摸事件方法,对触摸事件进行处理 *****/-(void)touchesBegan:(NSSet*)touches withEvent:(nullableUIEvent*)event;//触摸开始-(void)touchesMoved:(NSSet*)touches withEvent:(nullableUIEvent*)event;//触摸移动-(void)touchesEnded:(NSSet*)touches withEvent:(nullableUIEvent*)event;//触摸结束-(void)touchesCancelled:(nullableNSSet*)touches withEvent:(nullableUIEvent*)event;//触摸取消

    ios中主要的手势:

    1.UITapGestureRecognizer //单击手势

    �点击作为最常用手势,用于按下或选择一个控件或条目(类似于普通的鼠标点击)

    /**

    添加点击手势

    */-(void)addTapGestureWithTarget:(id)sender{//1,tap(点击)UITapGestureRecognizer*tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(tapGestureAction:)];//手势点击次数tapGesture.numberOfTapsRequired=1;// Default is 1//点击手指数量tapGesture.numberOfTouchesRequired=1;// Default is 1//将手势识别器添加到view上[sender addGestureRecognizer:tapGesture];}/**

    点击手势的响应方法

    */-(void)tapGestureAction:(UITapGestureRecognizer*)gesture{NSLog(@"%s",__FUNCTION__);UILabel*tempLabel=[self.view  viewWithTag:1001];NSInteger tapCount=gesture.numberOfTapsRequired;//点击次数NSInteger touchCount=gesture.numberOfTouchesRequired;//手指个数tempLabel.text=[NSString stringWithFormat:@"手指个数:%ld  点击次数:%ld",(long)touchCount,(long)tapCount];}

    2.UIPanGestureRecognizer //拖动手势

    拖动用于实现一些页面的滚动,以及对控件的移动功能。

    /**

    添加拖拽手势

    */-(void)addPanGestureWithView:(id)sender{//2,pan(平移)UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:selfaction:@selector(panGestureAction:)];panGesture.minimumNumberOfTouches=1;//最少点击次数---[sender addGestureRecognizer:panGesture];}/**

    拖拽手势相应方法

    */-(void)panGestureAction:(UIPanGestureRecognizer*)gesture{switch(gesture.state){//开始caseUIGestureRecognizerStateBegan:{NSLog(@"开始");}break;//改变caseUIGestureRecognizerStateChanged:{//1,改变redView的frameUILabel*redView=(UILabel*)gesture.view;//2,改变的坐标-移动的距离CGPoint point=[gesture translationInView:redView];NSLog(@"%@",NSStringFromCGPoint(point));//3,根据移动的距离改变redView的frame//CGRectOffset - 根据偏移量改变view的x值和y值redView.frame=CGRectOffset(redView.frame,point.x,point.y);//清空偏移量的累加值[gesture setTranslation:CGPointZero inView:redView];}break;//结束caseUIGestureRecognizerStateEnded:{NSLog(@"结束");}break;default:break;}NSLog(@"%s",__FUNCTION__);}

    3.UISwipeGestureRecognizer //轻扫手势

    横扫手势用于激活列表项的快捷操作菜单

    /**

    添加轻扫手势

    */-(void)addSwipeGestureWithView:(UIView*)aView{//3,swipe(轻扫)UISwipeGestureRecognizer*swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(swipeGestureAction:)];//8个方向//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];}/**

    轻扫手势响应方法

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

    4.UIPinchGestureRecognizer //捏合手势

    即放大缩小

    /**

    添加捏合手势

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

    捏合手势响应方法

    */-(void)pinchGestureAction:(UIPinchGestureRecognizer*)gesture{switch(gesture.state){caseUIGestureRecognizerStateBegan:{//手势开始//记录,当前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//边缘滑入

    滑动用于实现页面的快速滚动和翻页的功能。

    /**

    添加边缘滑入手势

    */-(void)addScreenEdgePanGestureWithView:(UIView*)aView{//5,ScreenEdgePan边缘划入UIScreenEdgePanGestureRecognizer*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];}/**

    边缘划入响应方法

    */-(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 //旋转手势

    即将视图围绕中心点转动

    /**

    添加旋转手势

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

    旋转手势响应方法

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

    7.UILongPressGestureRecognizer //长按手势

    将出现编辑菜单

    /**

    添加长按手势

    */-(void)addLongPressGestureWithView:(UIView*)aView{//7,longPress长按UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:selfaction:@selector(longPressAction:)];/* numberOfTouchesRequired这个属性保存了有多少个手指点击了屏幕,因此你要确保你每次的点击手指数目是一样的,默认值是为 0. */longPress.numberOfTouchesRequired=1;//手指个数//longPress.minimumPressDuration = 2;//按的最少时长/*最大100像素的运动是手势识别所允许的  Default is 10.*/longPress.allowableMovement=100;///*这个参数表示,两次点击之间间隔的时间长度。Default is 0.5.*/longPress.minimumPressDuration=1.0;[aView addGestureRecognizer:longPress];}/**

    长按相应方法

    */-(void)longPressAction:(UILongPressGestureRecognizer*)gesture{NSLog(@"%s",__FUNCTION__);//弹出menuswitch(gesture.state){caseUIGestureRecognizerStateBegan:{//UIMenuController menu控制器//系统的粘贴复制的小弹框//menuController  单例UIMenuController*ctr=[UIMenuController sharedMenuController];/*自定义Menu按钮

                //menu按钮

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

                UIMenuItem *mItem1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(longPressMenuAction)];

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

                //将item添加到controller中

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

                *///获得手指点击的位置CGPoint point=[gesture locationInView:gesture.view];//设置显示的位置[ctr setTargetRect:CGRectMake(point.x,point.y,0,0)inView:gesture.view];//显示menu工具条[ctr setMenuVisible:YES animated:YES];}break;default:break;}}

    相关文章

      网友评论

          本文标题:ios中主要的手势

          本文链接:https://www.haomeiwen.com/subject/gqzvtktx.html