IOS手势

[TOC]

## IOS****手势

学习ios手势,先要了解响应链,ios中只要继承UIResponse对象的都能成为事件的响应者,当用户操作手机屏幕时,所操作的控件就是第一响应者,操作事件一步步向下传递(如果该控件不做处理)

  • 事件响应链的传播路线:
 initial view –> super view –> …..–> view controller –> window –> Application –> AppDelegate
response响应链
IOS手势_第1张图片
  • 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:self action:@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:self action:@selector(panGestureAction:)];
    panGesture.minimumNumberOfTouches = 1;//最少点击次数---
    [sender addGestureRecognizer:panGesture];
}

/**
 拖拽手势相应方法
 */
-(void)panGestureAction:(UIPanGestureRecognizer*)gesture
{
    switch (gesture.state)
    {
            //开始
        case UIGestureRecognizerStateBegan:
        {
            NSLog(@"开始");
        }
            break;
            //改变
        case UIGestureRecognizerStateChanged:
        {
            //1,改变redView的frame
            UILabel *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;
            //结束
        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"结束");
        }
            break;
            
        default:
            break;
    }
    NSLog(@"%s",__FUNCTION__);
    
}

3.UISwipeGestureRecognizer //轻扫手势

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

/**
 添加轻扫手势
 */
- (void)addSwipeGestureWithView:(UIView *)aView {
    //3,swipe(轻扫)
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@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__);
    int  direction = gesture.direction;
    UILabel * tempLabel = (UILabel *)gesture.view;
    if (direction == 1) {
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:右"];
    }else if (direction == 2){
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:左"];
    
    }else if (direction == 3){
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:上"];
        
    }else if (direction == 4){
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:下"];
        
    }
}

4.UIPinchGestureRecognizer //捏合手势

即放大缩小

/**
 添加捏合手势
 */
- (void)addPinchGestureWithView:(UIView *)aView {

    //4,pinch捏和
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureAction:)];
    [self.pinchImg addGestureRecognizer:pinchGesture];
    
}
/**
 捏合手势响应方法
 */
-(void)pinchGestureAction:(UIPinchGestureRecognizer*)gesture
{
    switch (gesture.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            //手势开始
            //记录,当前view的frame,作为原始frame
            redViewRect = gesture.view.frame;
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            //1,拿到view
            UIView *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;
        case UIGestureRecognizerStateEnded:
        {
            
        }
            break;
            
        default:
            break;
    }
    
    
}

5.UIScreenEdgePanGestureRecognizer//边缘滑入

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

/**
 添加边缘滑入手势
 */
- (void)addScreenEdgePanGestureWithView:(UIView *)aView {

    //5,ScreenEdgePan边缘划入
    UIScreenEdgePanGestureRecognizer *sePanGesture = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@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];
    [self presentViewController:alert animated:YES completion:nil];
}

6.UIRotationGestureRecognizer //旋转手势

即将视图围绕中心点转动

/**
 添加旋转手势
 */
- (void)addRotationGestureWithView:(UIView *)aView {
    //6,rotation旋转
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@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:self action:@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__);
    //弹出menu
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            //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手势

ios事件处理

你可能感兴趣的:(IOS手势)