1. 開啟storyboard, 從元件褲拖曳一個Long Press Gesture Recognizer元件到view上,

並在viewcontroller.m檔中建立關聯性, 程式碼如下

@interfaceViewController ()

- (IBAction)handleLongPressGesture:(UILongPressGestureRecognizer *)sender;

@end

2. view controller上點選lomg press圖示,

可在attribute面板看到long press手勢有四個參數可設定,

Min duration為手指長按幾秒會觸發手勢元件的IBAction方法.

Recongnize中的Tap與Touches分別為numberOfTapsRequired與

numberOfTouchesRequired這兩個屬性,

代表多少根手指觸及螢幕多少次後會觸發IBAction方法.

Tolerance則對應到allowableMovement屬性,意思是觸發IBAction方法前容許手指移動多少pixel,

若手指移動超過這些數字手勢就會失敗, 預設值為10 pixel.

 

3. 在viewController.m檔中設定long press手指的IBAction方法,

傳入的參數sender型態改為UILongPressGestureRecongnizer.

- (IBAction)handleLongPressGesture:(UILongPressGestureRecognizer *)sender {

    for (int i = 0; i < sender.numberOfTouchesRequired; i++) {

        CGPoint point =[sender locationOfTouch:i inView:sender.view];

        //第%d根手指的位置在{x, x}

        NSLog(@"%d %@", (i+1), NSStringFromCGPoint(point) ); 
    }
}

4. 點擊command + r 執行,手指在矩型範圍內長按0.5秒觸發

 //viewController.m檔程式碼如下

#import "ViewController.h"


@interfaceViewController ()

- (IBAction)handleLongPressGesture:(UILongPressGestureRecognizer *)sender;

@end

@implementation ViewController

- (IBAction)handleLongPressGesture:(UILongPressGestureRecognizer *)sender {

    for (int i = 0; i < sender.numberOfTouchesRequired; i++) {
        CGPoint point =[sender locationOfTouch:i inView:sender.view];
        NSLog(@"%d %@", (i+1), NSStringFromCGPoint(point) );
    }
}

@end
arrow
arrow
    創作者介紹
    創作者 Will 的頭像
    Will

    Will的部落格

    Will 發表在 痞客邦 留言(0) 人氣()