[IOS]手势总结

1.UIPanGestureRecognizer 判断向左向右:

https://stackoverflow.com/questions/5187502/how-can-i-capture-which-direction-is-being-panned-using-uipangesturerecognizer

- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
//获取手势的加速度
    CGPoint velocity = [gestureRecognizer velocityInView:yourView];

    if(velocity.x > 0)
    {
        NSLog(@"gesture went right");
    }
    else
    {
        NSLog(@"gesture went left");
    }
}

 手势的几种状态:

UIGestureRecognizerStateBegan:移动开始

UIGestureRecognizerStateChanged:移动中

UIGestureRecognizerStateEnded:移动结束

 

2.新建单击手势:

声明:

@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
 方法:
#pragma mark - tap gesture method
-(UITapGestureRecognizer *)tapGesture{
    if (!_tapGesture) {
        _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)];
//单击
        [_tapGesture setNumberOfTapsRequired:1];
        
    }
    return _tapGesture;
}

//触发
- (void)handleTapAction:(UITapGestureRecognizer *)tapGesture{

    [self resetShowType:PSDrawerManagerShowCenter];
    //刷新UI,有时需要异步
    dispatch_async(dispatch_get_main_queue(), ^{
        [self callNotificationReceiver:@"show_center"];
    });
}

-(void)addTapGesture{
    if (!_tapGesture) {
//注意:这里要使用self.tapGesture而不是_tapGesture,要不会报错
        [_cover_view addGestureRecognizer:self.tapGesture];
    }
}

-(void)removeTapGesture{
    if (_tapGesture) {
        [self.centerViewController.view removeGestureRecognizer:_tapGesture];
    }
}
 

猜你喜欢

转载自jameskaron.iteye.com/blog/2394448