美文网首页
UIGesture扩展手势:UIPan平移、UISwipe滑动、

UIGesture扩展手势:UIPan平移、UISwipe滑动、

作者: 李琪_59dc | 来源:发表于2017-10-13 15:09 被阅读0次
    • UIPanGestureRecognizer
      平移手势:可以用手指在屏幕上移动的手势
    • UISwipeGestureRecognizer
      滑动手势:左滑、右滑、上滑、下滑
    • UILongPressGestureRecognizer
      长按手势:长时间按住一个视图响应事件

    重点属性

    • minimumPressDuration 长按事件长度
    • direction 滑动手势方向
    • UIGestureRecognizerStateBegan 长按事件状态
    • translationInView 获取平移手势位置
    • velocityInView 获取平移手势速度

    UIPanGestureRecognizer平移手势

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIImageView *iView = [[UIImageView alloc]initWithImage:[UIImage           imageNamed:@"icon1"]];
        iView.frame = CGRectMake(50, 50, 200, 300);
        iView.userInteractionEnabled = YES;
        [self.view addSubview:iView];
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer   alloc]initWithTarget:self action:@selector(panAct:)];
        [iView addGestureRecognizer:pan];
    }
    
    //移动事件函数,只要手指坐标在屏幕上发生变化时,就调用
    -(void)panAct:(UIPanGestureRecognizer*)pan{
        //获取移动的坐标,相对于视图的坐标系
        //参数:相对的视图对象
        CGPoint pt = [pan translationInView:self.view];
        NSLog(@"pan!!");
        NSLog(@"pt.x = %.2f, pt.y = %.2f",pt.x,pt.y);
        
        CGPoint pv = [pan velocityInView:self.view];
        NSLog(@"pt.x = %.2f, pt.y = %.2f",pv.x,pv.y);
    }
    

    UISwipeGestureRecognizer滑动手势

    - (void)viewDidLoad {
        [super viewDidLoad];
    //创建一个滑动手势
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAct:)];
        //设定滑动手势接受事件的类型
        //UISwipeGestureRecognizerDirectionLeft向左滑动
        //UISwipeGestureRecognizerDirectionRight
        //UISwipeGestureRecognizerDirectionUp
        //UISwipeGestureRecognizerDirectionDown
        swipe.direction = UISwipeGestureRecognizerDirectionLeft;
        //不能写一起 swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
        //要分开创建,然后遵守手势协议,实现“是否支持多个手势”
        [iView addGestureRecognizer:swipe];
    }
    -(void)swipeAct:(UISwipeGestureRecognizer*)swipe{
        NSLog(@"向左滑动");
    }
    

    UILongPressGestureRecognizer长按手势

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressLong:)];
        [iView addGestureRecognizer:longPress];
        
        //设置长按手势的时间,默认值0.5s
        longPress.minimumPressDuration = 0.5;
    }
    
    -(void)pressLong:(UILongPressGestureRecognizer*)press{
        //到达时间调用一次,手指离开又调用一次
        NSLog(@"长按手势llllllll");
        if(press.state == UIGestureRecognizerStateBegan){
            NSLog(@"状态开始");
        }
        else if(press.state == UIGestureRecognizerStateEnded){
            NSLog(@"状态结束");
        }
    }
    

    相关文章

      网友评论

          本文标题:UIGesture扩展手势:UIPan平移、UISwipe滑动、

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