iOS開發 - 手勢識別(UIGestureRecognizer)

來源:互聯網
上載者:User

iOS開發 - 手勢識別(UIGestureRecognizer)
UIGestureRecognizer

為了完成手勢識別,必須藉助於手勢辨識器—-UIGestureRecognizer

利用UIGestureRecognizer,能輕鬆識別使用者在某個view上面做的一些常見手勢

UIGestureRecognizer是一個抽象類別,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢

UITapGestureRecognizer       //(敲擊)UIPinchGestureRecognizer     //(捏合,用於縮放)UIPanGestureRecognizer       //(拖拽)UISwipeGestureRecognizer     //(輕掃)UIRotationGestureRecognizer  //(旋轉)UILongPressGestureRecognizer //(長按)
敲擊 UITapGestureRecognizer

每一個手勢辨識器的用法都差不多,比如UITapGestureRecognizer的使用步驟如下

建立手勢辨識器對象UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];設定手勢辨識器對象的具體屬性// 連續敲擊2次tap.numberOfTapsRequired = 2;// 需要2根手指一起敲擊tap.numberOfTouchesRequired = 2;添加手勢辨識器到對應的view上[self.iconView addGestureRecognizer:tap];監聽手勢的觸發[tap addTarget:self action:@selector(tapIconView:)];
敲擊執行個體
@interface GRViewController () @property (weak, nonatomic) IBOutlet UIImageView *iconView;@end@implementation GRViewController- (void)viewDidLoad{    [super viewDidLoad];    [self testTap2];}- (void)testTap2{    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];    tap.delegate = self;    [self.iconView addGestureRecognizer:tap];}#pragma mark - 代理方法/** *  當點擊view的時候,會先調用這個方法 */- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{    CGPoint pos = [touch locationInView:touch.view];    if (pos.x <= self.iconView.frame.size.width * 0.5) {        return YES;    }    return NO;}- (void)testTap{    // 1.建立手勢辨識器對象    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];    // 連續敲擊2次,手勢才能識別成功    tap.numberOfTapsRequired = 2;    tap.numberOfTouchesRequired = 2;    // 2.添加手勢辨識器對象到對應的view    [self.iconView addGestureRecognizer:tap];    // 3.添加監聽方法(識別到了對應的手勢,就會調用監聽方法)    [tap addTarget:self action:@selector(tapView)];}- (void)tapView{    NSLog(@"-----我敲擊了螢幕!!");}@end
長按和輕掃執行個體
@interface GRViewController ()@property (weak, nonatomic) IBOutlet UIView *redView;@end@implementation GRViewController- (void)viewDidLoad{    [super viewDidLoad];    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView)];    swipe.direction = UISwipeGestureRecognizerDirectionUp;    [self.redView addGestureRecognizer:swipe];}- (void)swipeView{    NSLog(@"swipeView");}- (void)testLongPress{    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];    [longPress addTarget:self action:@selector(longPressView)];    // 至少長按2秒    longPress.minimumPressDuration = 2;    // 在觸發手勢之前,50px範圍內長按有效    longPress.allowableMovement = 50;    [self.redView addGestureRecognizer:longPress];}- (void)longPressView{    NSLog(@"長按了紅色的view");}@end
縮放和旋轉執行個體
@interface GRViewController () @property (weak, nonatomic) IBOutlet UIImageView *iconView;@end@implementation GRViewController- (void)viewDidLoad{    [super viewDidLoad];    [self testPinchAndRotate];}#pragma mark - 手勢辨識器的代理方法/** *  是否允許多個手勢辨識器同時有效 *  Simultaneously : 同時地 */- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{    return YES;}#pragma mark - 縮放 + 旋轉- (void)testPinchAndRotate{    [self testPinch];    [self testRotate];}#pragma mark - 縮放手勢(捏合手勢)- (void)testPinch{    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];    pinch.delegate = self;    [self.iconView addGestureRecognizer:pinch];}- (void)pinchView:(UIPinchGestureRecognizer *)pinch{    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);    pinch.scale = 1; // 這個真的很重要!!!!!}#pragma mark - 旋轉手勢- (void)testRotate{    UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];    recognizer.delegate = self;    [self.iconView addGestureRecognizer:recognizer];}- (void)rotateView:(UIRotationGestureRecognizer *)recognizer{    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);    recognizer.rotation = 0; // 這個很重要!!!!!}@end
拖拽執行個體
@interface GRViewController ()@property (weak, nonatomic) IBOutlet UIView *purpleView;@end@implementation GRViewController- (void)viewDidLoad{    [super viewDidLoad];    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];    [self.purpleView addGestureRecognizer:pan];}- (void)panView:(UIPanGestureRecognizer *)pan{    switch (pan.state) {        case UIGestureRecognizerStateBegan: // 開始觸發手勢            break;        case UIGestureRecognizerStateEnded: // 手勢結束            break;        default:            break;    }    // 1.在view上面挪動的距離    CGPoint translation = [pan translationInView:pan.view];    CGPoint center = pan.view.center;    center.x += translation.x;    center.y += translation.y;    pan.view.center = center;    // 2.清空移動的距離    [pan setTranslation:CGPointZero inView:pan.view];}@end
手勢識別的狀態
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {    // 沒有觸摸事件發生,所有手勢識別的預設狀態    UIGestureRecognizerStatePossible,    // 一個手勢已經開始但尚未改變或者完成時    UIGestureRecognizerStateBegan,    // 手勢狀態改變    UIGestureRecognizerStateChanged,    // 手勢完成    UIGestureRecognizerStateEnded,    // 手勢取消,恢複至Possible狀態    UIGestureRecognizerStateCancelled,     // 手勢失敗,恢複至Possible狀態    UIGestureRecognizerStateFailed,    // 識別到手勢識別    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded};

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.