手势识别

一.iOS支持的手势识别

1.Tap点击
2.Long Press长按
3.Swipe轻扫
4.Pan滑动
5.Screen Edge Pan屏幕边缘滑动
6.Pinch放大缩小
7.Rotation旋转

二.Tap点击手势

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    //需要点击的次数,本例设为2,双击
    tapRecognizer.numberOfTapsRequired = 2;
    //两根手指点击,用option键可模拟两根手指
    tapRecognizer.numberOfTouchesRequired = 2;
    [self.blueView addGestureRecognizer:tapRecognizer];
    
}

-(void)tapGesture:(UITapGestureRecognizer *)sender {
    NSLog(@"view tapped");
}

三.Long Press长按手势

-(void)addLongPressGesture {
    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
    [self.blueView addGestureRecognizer:longPressRecognizer];
}

-(void)longPressGesture:(UILongPressGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        //长按开始
        NSLog(@"longpress began");        
    }else if (sender.state == UIGestureRecognizerStateEnded)
        //长按结束
        NSLog(@"longPress ended");
}

四.Swipe轻扫手势

-(void)addSwipeGesture {
    UISwipeGestureRecognizer *swipeDownRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
    /*
     默认从左向右轻扫,即如果不设置direction属性,只有从左向右轻扫有效,其余方向无效
     typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
     };
     */
    
    swipeDownRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
    [self.blueView addGestureRecognizer:swipeDownRecognizer ];
    //如果想同时添加两个方向,如向下和向右,需要添加两个UISwipeGestureRecognizer
    UISwipeGestureRecognizer *swipeRightRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture:)];
    swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.blueView addGestureRecognizer:swipeRightRecognizer ];
}

-(void)swipeGesture:(UISwipeGestureRecognizer *)sender {
    //区分
    if (sender.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"swiped down");
    } else if (sender.direction == UISwipeGestureRecognizerDirectionRight)
    NSLog(@"swiped right");
}

五.Pan滑动手势

-(void)addPanGesture {
    //1.创建UIPanGestureRecognizer实例,并调用initWithTarget
    //2.为blueView增加PanGesture手势
    //3.创建对应的PanGesture方法
    
    //targer:手势触发的目标-self
    //action:手势触发时调用的方法
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    [self.blueView addGestureRecognizer:panGes];
}

-(void)panGesture:(UIPanGestureRecognizer *)sender {
    //获取滑动的点
    CGPoint point = [sender locationInView:self.blueView];
    //获取当前滑动的速度
    CGPoint speed = [sender velocityInView:self.blueView];
    NSLog(@"point:%f,%f     speed:%f,%f",point.x,point.y,speed.x,speed.y); 
}

六.Screen Edge Pan屏幕边缘滑动手势

-(void)addScreenEdgePanGesture {
    UIScreenEdgePanGestureRecognizer *screenEdgePanGes = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanGesture:)];
    
    //设置屏幕边缘滑动触发的边缘,本例是屏幕右边缘
    screenEdgePanGes.edges = UIRectEdgeRight;
    //blueView不在屏幕边缘,所有添加在self.view中
    [self.view addGestureRecognizer:screenEdgePanGes];
}

-(void)screenEdgePanGesture:(UIScreenEdgePanGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"屏幕边缘滑动手势触发开始");
    } else if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"屏幕边缘滑动手势触发结束");
    }
}

七.Pinch缩放手势

-(void)addPinchGesture {
    UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGesture:)];
    
    [self.blueView addGestureRecognizer:pinchGes];
}

-(void)pinchGesture:(UIPinchGestureRecognizer *)sender {
    CGFloat scale =sender.scale; //缩放比例
    NSLog(@"pinch scale:%f",scale);
}

八.Rotation旋转手势

-(void)addRotationGesture {
    UIRotationGestureRecognizer *rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
    
    [self.blueView addGestureRecognizer:rotationGes];
}

-(void)rotationGesture:(UIRotationGestureRecognizer *)sender {
    CGFloat rotation = sender.rotation;//旋转角度
    NSLog(@"rotation:%f",rotation);
}

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