1. 程式人生 > >iOS開發之自定義手勢

iOS開發之自定義手勢

iOS為手指觸碰事件提供了手勢處理器,通過手勢處理器可使用一致的變成模式來處理各種觸碰事件,而且變成更加簡單,因此一般推薦使用者使用手勢來處理使用者的觸碰事件。無論處理哪種手勢,都可面向UIGestureRecognizer程式設計,UIGestureRecognizer提供如下子類:

UITapGestureRecognizer:點選手勢

UIPinchGestureRecognizer:捏合手勢

UIRotationGestureRecognizer:旋轉手勢

UISwipeGestureRecognizer:滑動手勢

UIPanGestureRecognizer:拖動手勢

UILongPressGestureRecognizer:長按手勢

但使用者也可以自定義手勢來處理指定的觸碰事件,具體步驟:

1.建立繼承自UIGestureRecognizer的類

2.重寫有關觸碰事件的4個方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
注意:要包含一個頭檔案:#import <UIKit/UIGestureRecognizerSubclass.h>

下面就具體自定義一個手勢,並使用。

該手勢(HXSwingGestureRecognizer)為,一根手勢在檢視上左右滑動,左右滑動超過一定次數就觸發該手勢。

HXSwingGestureRecognizer.h

#import <UIKit/UIKit.h>

@interface HXSwingGestureRecognizer : UIGestureRecognizer
/** 左右滑動的次數*/
@property (nonatomic, assign) NSUInteger swingCount;

@end
HXSwingGestureRecognizer.m
//
//  HXSwingGestureRecognizer.m
//  FK_01_自定義手勢
//
//  Created by shihuaixing on 16/3/7.
//  Copyright © 2016年 com.fang. All rights reserved.
//  該手勢用於處理“擺動”;即當手指在檢視上擺動超過一定次數,就觸發響應。

#import "HXSwingGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>

@implementation HXSwingGestureRecognizer
CGFloat baseY;// 記錄起始點的Y座標,左右輕掃的時候,上下方向不能超過一定的範圍。
CGFloat prevX;
NSInteger count;
NSUInteger prevDir;// 定義手勢移動的方向,1代表向右;2代表向左
// 重寫UIGestureRecognizer基類與觸控有關的4個方法




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 獲得任一觸碰點
    UITouch *touch = [touches anyObject];
    //獲取觸碰點在檢視上的座標
    CGPoint point = [touch locationInView:self.view];
    baseY = point.y;
    prevX = point.x;
    prevDir = 0;
    count = 0;
    
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    // 獲取當前觸碰點的座標
    CGPoint currentPoint = [touch locationInView:self.view];
    // 如果上下方向移動的距離過大,則取消手勢
    if (fabs(currentPoint.y - baseY) > 10) {
        [self setState:UIGestureRecognizerStateCancelled];
        
    }
    
    NSUInteger currenDir = currentPoint.x > prevX ? 1 : 2;
    // 剛開始還沒有初始化方向時
    if (prevDir == 0) {
        prevDir = currenDir;
    }
    if (prevDir != currenDir) {
        // 將“擺動”次數加1
        count ++;
        // 使用prevDir記錄當前擺動方向
        prevDir = currenDir;
    }
    if (count >= self.swingCount) {
        [self setState:UIGestureRecognizerStateEnded];
    }
    
}
//
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    
//}
//
//
//- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    return;
//}


@end
使用該手勢:
//
//  ViewController.m
//  FK_01_自定義手勢
//
//  Created by shihuaixing on 16/3/7.
//  Copyright © 2016年 com.fang. All rights reserved.
//

#import "ViewController.h"
#import "HXSwingGestureRecognizer.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    HXSwingGestureRecognizer *swing = [[HXSwingGestureRecognizer alloc] initWithTarget:self action:@selector(swingAction)];
    swing.swingCount = 3;
    [self.view addGestureRecognizer:swing];
}
- (void)swingAction {
    NSLog(@"開始swing");
}
@end
完成!