iOS開發之手勢識別

來源:互聯網
上載者:User

標籤:

一,用storyboard給控制項添加手勢識別

1.用storyboard添加手勢識別,和添加一個Button的步驟一樣,首先我們得找到相應的手勢,把手勢識別的控制項拖到我們要添加手勢的控制項中,如下:

2.給我們拖出的手勢添加回調事件,和給Button回調事件沒啥區別的,在回調方法中添加要實現的商務邏輯即可,如下:

 

二,純程式碼添加手勢識別

1.輕擊手勢(TapGestureRecognizer)的添加

初始化代碼TapGestureRecongnizer的代碼如下:

123456 //建立tap手勢    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];    //設定點擊次數和點擊手指數    tapGesture.numberOfTapsRequired = 1; //點擊次數    tapGesture.numberOfTouchesRequired = 1; //點擊手指數    [self.view addGestureRecognizer:tapGesture];

在回調方法中添加相應的商務邏輯:

12345 //輕擊手勢觸發方法-(void)tapGesture:(id)sender{    //輕擊後要做的事情        }

2.長按手勢(LongPressGestureRecognizer)
初始化代碼:

12345     //添加長摁手勢    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];    //設定長按時間    longPressGesture.minimumPressDuration = 0.5; //(2秒)    [self.view addGestureRecognizer:longPressGesture];

在對應的回調方法中添加相應的方法(當手勢開始時執行):

12345678910 //常摁手勢觸發方法-(void)longPressGesture:(id)sender{    UILongPressGestureRecognizer *longPress = sender;    if (longPress.state == UIGestureRecognizerStateBegan)    {        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"長按觸發" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];        [alter show];    }}

代碼說明:手勢的常用狀態如下

  • 開始:UIGestureRecognizerStateBegan

  • 改變:UIGestureRecognizerStateChanged

  • 結束:UIGestureRecognizerStateEnded

  • 取消:UIGestureRecognizerStateCancelled

  • 失敗:UIGestureRecognizerStateFailed

3.撥動手勢(SwipeGestureRecognizer)

在初始化撥動手勢的時候得指定輕掃的方向,上下左右。 如果要要添加多個輕掃方向,就得添加多個撥動手勢,不過回調的是同一個方法。

添加撥動手勢,一個向左一個向右,代碼如下:

1234567891011 //添加撥動手勢    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];    //設定輕掃的方向    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //預設向右    [self.view addGestureRecognizer:swipeGesture];         //添加撥動手勢    UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];    //設定輕掃的方向    swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //預設向右    [self.view addGestureRecognizer:swipeGestureLeft];

回調方法如下:

12345678910111213 //撥動手勢觸發方法-(void)swipeGesture:(id)sender{    UISwipeGestureRecognizer *swipe = sender;    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)    {        //向左輕掃做的事情    }    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)    {        //向右輕掃做的事情    }}

4.捏合手勢(PinchGestureRecognizer)

捏合手勢初始化

123 //添加捏合手勢UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];[self.view addGestureRecognizer:pinchGesture];

捏合手勢要觸發的方法(放大或者縮小圖片):

1234567891011121314151617181920 ////捏合手勢觸發方法-(void) pinchGesture:(id)sender{     UIPinchGestureRecognizer *gesture = sender;         //手勢改變時    if (gesture.state == UIGestureRecognizerStateChanged)    {        //捏合手勢中scale屬性記錄的縮放比例        _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);    }         //結束後恢複    if(gesture.state==UIGestureRecognizerStateEnded)    {        [UIView animateWithDuration:0.5 animations:^{            _imageView.transform = CGAffineTransformIdentity;//取消一切形變        }];    }}

5.拖動手勢(PanGestureRecognizer)

拖動手勢的初始化

123 //添加拖動手勢UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];[self.view addGestureRecognizer:panGesture];

拖動手勢要做的方法(通過translationInView擷取移動的點,和TouchesMoved方法類似)

123456789 //拖動手勢-(void) panGesture:(id)sender{    UIPanGestureRecognizer *panGesture = sender;         CGPoint movePoint = [panGesture translationInView:self.view];         //做你想做的事兒}

6.旋轉手勢(RotationGestureRecognizer)

旋轉手勢的初始化

123 //添加旋轉手勢UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];[self.view addGestureRecognizer:rotationGesture];

旋轉手勢調用的方法:

1234567891011121314151617181920 //旋轉手勢-(void)rotationGesture:(id)sender{         UIRotationGestureRecognizer *gesture = sender;         if (gesture.state==UIGestureRecognizerStateChanged)    {        _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);    }         if(gesture.state==UIGestureRecognizerStateEnded)    {                 [UIView animateWithDuration:1 animations:^{            _imageView.transform=CGAffineTransformIdentity;//取消形變        }];    }     }

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.