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
完成!