標籤: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手勢辨識器