天天看点

UIKit框架-基本视图-UIGestureRecognizer-手势

UIGestureRecognizer 手势

1.UITapGestureRecognizer 点击手势
// 初始化点击手势,添加点击方法
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    // 设置点击的次数
    tap.numberOfTapsRequired = ;
    // 设置点击的手指数
    tap.numberOfTouchesRequired = ;
    // 主视图添加点击手势
    [self.view addGestureRecognizer:tap];

- (void)tap:(UITapGestureRecognizer *)tap {   
}
           
2.UISwipeGestureRecognizer 滑动手势
// 初始化滑动手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    // 设置滑动方法
    /** UISwipeGestureRecognizerDirection 滑动方法
    UISwipeGestureRecognizerDirectionRight
    UISwipeGestureRecognizerDirectionLeft
    UISwipeGestureRecognizerDirectionUp    
    UISwipeGestureRecognizerDirectionDown
     */
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipe];
- (void)swipe:(UISwipeGestureRecognizer *)swipe {
    switch (swipe.direction) {
        case UISwipeGestureRecognizerDirectionUp:
            break;
        case UISwipeGestureRecognizerDirectionDown:
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            break;
        case UISwipeGestureRecognizerDirectionRight:
            break;
        default:
            break;
    }
}
           
3.UILongPressGestureRecognizer 长按手势
// 初始化长按手势
   UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpress:)];
    // 设置长按的最小时间
    longpress.minimumPressDuration = ;
    // 设置允许移动的距离
    longpress.allowableMovement = ;
    [self.view addGestureRecognizer:longpress];
- (void)longpress:(UILongPressGestureRecognizer *)longpress {   
}
           
4.UIPanGestureRecognizer 拖动手势
初始化拖动手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
- (void)pan:(UIPanGestureRecognizer *)pan {
    /** UIGestureRecognizerState 状态
     UIGestureRecognizerStateBegan 开始拖动
     UIGestureRecognizerStateChanged 正在拖动
     UIGestureRecognizerStateEnded 结束拖动
     */
    // 获取手势在view上的位置
    CGPoint point = [pan translationInView:view];
    // 改变view上的x、y值
    [pan setTranslation:point inView:view];
    // 获取在view上拖动的速度
    CGPoint point = [pan velocityInView:view];
}
           
5.UIPinchGestureRecognizer 捏合手势
// 初始化捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    // 设置比例
    pinch.scale = ;
    [self.view addGestureRecognizer:pinch];
- (void)pinch:(UIPinchGestureRecognizer *)pinch {
    // 获取手势的速度
    CGFloat velocity = pinch.velocity;
}
           
6.UIRotationGestureRecognizer 旋转手势
// 初始化旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    // 设置旋转的角度
    rotation.rotation = ;
    [self.view addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation {
}
           
7.UIScreenEdgePanGestureRecognizer 边缘手势
// 初始化边缘手势
    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePan:)];
    // 设置边缘范围
    /** UIRectEdge 范围
     UIRectEdgeNone
     UIRectEdgeTop
     UIRectEdgeLeft
     UIRectEdgeBottom
     UIRectEdgeRight
     UIRectEdgeAll
     */
    edgePan.edges = UIRectEdgeNone;
    [self.view addGestureRecognizer:edgePan];
-(void)edgePan:(UIScreenEdgePanGestureRecognizer *)edgePan {
}