iOS的基本手勢

來源:互聯網
上載者:User

標籤:

1.點選手勢:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
    //設定點擊次序,識別手勢的時候使用
    tapGesture.numberOfTapsRequired = 2;

2.長按手勢:

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture:)];
    //設定按壓最短期間,預設0.5s
    longPress.minimumPressDuration = 1;

3.滑動手勢:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
    [view addGestureRecognizer:panGesture];

4.捏合手勢:

 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
    pinchGesture.delegate = self;
5.旋轉手勢:

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture:)];
    rotationGesture.delegate = self;
#pragma mark -
#pragma mark Handle Gesture Recoginzer

- (void)handleRotationGesture:(UIRotationGestureRecognizer*)gesture{
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        UIView *view = gesture.view;
        [self.view bringSubviewToFront:view];
        //旋轉的弧度
        CGFloat rotation = gesture.rotation;
        view.transform = CGAffineTransformRotate(view.transform, rotation);
        
        //注意有累加
        gesture.rotation = 0.0;
    }
}

- (void)handlePinchGesture:(UIPinchGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        UIView *view = gesture.view;
        [self.view bringSubviewToFront:view];
        //得到縮放的比例,然後設定transform的縮放參數
        //gesture.scale 捏合時候的縮放比例
        CGFloat scale = gesture.scale;
        
        //設定view.transform scale 代表縮放
        //第一個參數,是基準值,在此基礎上進行縮放
        //第二個參數,x 座標方向的縮放比例
        //第三個參數,y 座標方向的縮放比例
        view.transform = CGAffineTransformScale(view.transform, scale, scale);
        
        //由於手勢效果會累積,所以需要給他複位
        gesture.scale = 1.0;
    }
}

- (void)handlePanGesture:(UIPanGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        //手勢識別成功,並且手勢持續進行中
        UIView *view = gesture.view;
        [self.view bringSubviewToFront:view];
        //手勢在self.view上移動的多少
        CGPoint offset = [gesture translationInView:self.view];
        CGPoint newCenter = CGPointMake(view.center.x+offset.x, view.center.y+offset.y);
        view.center = newCenter;
        
        //手勢的效果會累加,translation一直累加
        //重新初始化,去除原來的累加效果
        [gesture setTranslation:CGPointZero inView:self.view];
    }
}

//點選手勢處理
- (void)handleTapGesture:(UITapGestureRecognizer*)gesture
{
    //gesture.view; 代表手勢發生在哪一個view上
    NSLog(@"點擊了2次,手勢識別成功");
}

//手勢識別的過程,首先接受到touchbegin的事件,開始啟動手勢識別,手勢的狀態
//UIGestureRecognizerStatePossible,然後繼續接收並分析觸摸事件,如果手勢識別成功,手勢的狀態變為UIGestureRecognizerStateBegan
//如果手勢繼續進行,手勢的狀態變為UIGestureRecognizerStateChanged
//如果手勢結束,狀態變為UIGestureRecognizerStateEnded
//如果手勢識別失敗 UIGestureRecognizerStateFailed
- (void)handleLongPressGesture:(UILongPressGestureRecognizer*)gestrue
{
    if (gestrue.state == UIGestureRecognizerStateBegan || gestrue.state == UIGestureRecognizerStateChanged) {
        NSLog(@"長按手勢識別成功");
    }
    if (gestrue.state == UIGestureRecognizerStateEnded) {
        NSLog(@"手勢識別結束");
    }
}

#pragma mark -
#pragma mark UIGestureReconigerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (gestureRecognizer.view == otherGestureRecognizer.view) {
        return YES;
    }
    return NO;
}


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.