你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(71)之長按手勢

IOS開發(71)之長按手勢

編輯:IOS開發綜合

1 前言

UILongPressGestureRecognizer 用來監聽並捕獲到用戶用手指長久按住屏幕的某一個地方的手勢事件。


2 代碼實例
ZYViewController.m

 

[plain]
- (void)viewDidLoad 

    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    self.dummyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    self.dummyButton.frame = CGRectMake(0.0f,0.0f, 100.0f, 37.0f); 
    self.dummyButton.center = self.view.center; 
    [self.dummyButton setTitle:@"注意我的位置" forState:UIControlStateNormal]; 
    [self.view addSubview:self.dummyButton]; 
    /*第一次創建手勢識別器*/ 
    self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] 
                                       initWithTarget:self 
                                       action:@selector(handleLongPressGestures:)]; 
    /* numberOfTouchesRequired這個屬性保存了有多少個手指點擊了屏幕,因此你要確保你每次的點擊手指數目是一樣的,默認值是為 0. */ 
    self.longPressGestureRecognizer.numberOfTouchesRequired = 2; 
    /* Maximum 100 pixels of movement allowed before the gesture is recognized */ 
    /*最大100像素的運動是手勢識別所允許的*/ 
    self.longPressGestureRecognizer.allowableMovement = 100.0f; 
    /*這個參數表示,兩次點擊之間間隔的時間長度。*/ 
    self.longPressGestureRecognizer.minimumPressDuration = 1.0; 
    [self.view addGestureRecognizer:self.longPressGestureRecognizer]; 

 
- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{ 
    if ([paramSender isEqual:self.longPressGestureRecognizer]){ 
        //如果按下手指數為2 
        if (paramSender.numberOfTouchesRequired == 2){ 
            /********獲得兩個手指間的中點 Start********/ 
            CGPoint touchPoint1 = [paramSender locationOfTouch:0 inView:paramSender.view]; 
            CGPoint touchPoint2 = [paramSender locationOfTouch:1 inView:paramSender.view]; 
            CGFloat midPointX = (touchPoint1.x + touchPoint2.x) / 2.0f; 
            CGFloat midPointY = (touchPoint1.y + touchPoint2.y) / 2.0f; 
            CGPoint midPoint = CGPointMake(midPointX, midPointY); 
            /********獲得兩個手指間的中點 End********/ 
            self.dummyButton.center = midPoint; 
        } 
    } 

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dummyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.dummyButton.frame = CGRectMake(0.0f,0.0f, 100.0f, 37.0f);
    self.dummyButton.center = self.view.center;
    [self.dummyButton setTitle:@"注意我的位置" forState:UIControlStateNormal];
    [self.view addSubview:self.dummyButton];
    /*第一次創建手勢識別器*/
    self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(handleLongPressGestures:)];
    /* numberOfTouchesRequired這個屬性保存了有多少個手指點擊了屏幕,因此你要確保你每次的點擊手指數目是一樣的,默認值是為 0. */
    self.longPressGestureRecognizer.numberOfTouchesRequired = 2;
    /* Maximum 100 pixels of movement allowed before the gesture is recognized */
    /*最大100像素的運動是手勢識別所允許的*/
    self.longPressGestureRecognizer.allowableMovement = 100.0f;
    /*這個參數表示,兩次點擊之間間隔的時間長度。*/
    self.longPressGestureRecognizer.minimumPressDuration = 1.0;
    [self.view addGestureRecognizer:self.longPressGestureRecognizer];
}

- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
    if ([paramSender isEqual:self.longPressGestureRecognizer]){
        //如果按下手指數為2
        if (paramSender.numberOfTouchesRequired == 2){
            /********獲得兩個手指間的中點 Start********/
            CGPoint touchPoint1 = [paramSender locationOfTouch:0 inView:paramSender.view];
            CGPoint touchPoint2 = [paramSender locationOfTouch:1 inView:paramSender.view];
            CGFloat midPointX = (touchPoint1.x + touchPoint2.x) / 2.0f;
            CGFloat midPointY = (touchPoint1.y + touchPoint2.y) / 2.0f;
            CGPoint midPoint = CGPointMake(midPointX, midPointY);
            /********獲得兩個手指間的中點 End********/
            self.dummyButton.center = midPoint;
        }
    }
}
運行結果

 \
 


兩個手指長按後的結果(也可以使用Option+鼠標長按)

 \
 


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved