iOS手勢辨識器

來源:互聯網
上載者:User

標籤:for   長按   add   ebe   nsf   識別   cgpoint   文章   anim   

UIGestureRecognizer

UIGestureRecognizer類,用於檢測、識別使用者使用裝置時所用的手勢.它是一個抽象類別,定義了所有手勢的基本行為.以下是UIGestureRecognizer子類,用於處理具體的使用者手勢行為:

UITapGestureRecognizer // 1.單擊
UILongPressGestureRecognizer // 3.長按
UISwipeGestureRecognizer // 4.輕掃
UIPanGestureRecognizer // 5.移動
UIRotationGestureRecognizer // 6.旋轉
UIPinchGestureRecognizer // 7.捏合

建立手勢:
    // 1.單擊    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];    [imgView addGestureRecognizer:tap];        // 2.雙擊    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];    doubleTap.numberOfTapsRequired = 2;    [imgView addGestureRecognizer:doubleTap];        // 雙擊失敗才單擊    [tap requireGestureRecognizerToFail:doubleTap];        // 3.長按    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];    //設定最短時間    longPress.minimumPressDuration = 1;    [imgView addGestureRecognizer:longPress];            // 4.輕掃    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];    // 設定輕掃方向    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];    [imgView addGestureRecognizer:swipe];        // 5.移動    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];    [imgView addGestureRecognizer:pan];        // 輕掃失敗才移動    [pan requireGestureRecognizerToFail:swipe];        // 6.旋轉    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];    [imgView addGestureRecognizer:rotation];        // 7.捏合    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];    [imgView addGestureRecognizer:pinch];
手勢觸發事件:

GestureAction:

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{    if (longPress.state == UIGestureRecognizerStateBegan) {        NSLog(@"長按開始");    }else if (longPress.state == UIGestureRecognizerStateEnded){        NSLog(@"長按結束");    }}- (void)panAction:(UIPanGestureRecognizer *)pan {    //手指所在的座標    CGPoint point = [pan locationInView:self.view];    _view.center = point;}- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{    if (rotation.state == UIGestureRecognizerStateChanged) {                //取到弧度        CGFloat angle = rotation.rotation;                //正在旋轉        rotation.view.transform = CGAffineTransformMakeRotation(angle);            } else if (rotation.state == UIGestureRecognizerStateEnded) {                //還原        [UIView animateWithDuration:.5 animations:^{                        rotation.view.transform = CGAffineTransformIdentity;        }];    }}- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{    if (pinch.state == UIGestureRecognizerStateChanged) {                // 取到縮放比率        CGFloat scale = pinch.scale;                // 縮放        pinch.view.transform = CGAffineTransformMakeScale(scale, scale);            } else if (pinch.state == UIGestureRecognizerStateEnded) {                [UIView animateWithDuration:.5 animations:^{                        pinch.view.transform = CGAffineTransformIdentity;        }];    }}
Motion 搖晃手勢 
//讓當前對象成為第一響應者- (BOOL)canBecomeFirstResponder{             return YES;}- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{   NSLog(@"搖一搖開始");}- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{   NSLog(@"搖一搖結束");}

推薦一篇iOS手勢識別的詳細使用的文章:iOS手勢識別

iOS手勢辨識器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.