天天看點

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 {
}