核心1. 多种手势嵌套
单击:UITapGestureRecognizer *singleTap; // 一般处理 播放控制页面显示/隐藏
双击:UITapGestureRecognizer *doubleTap; // 一般处理 暂停/播放
滑动:UIPanGestureRecognizer *panGR; // 一般处理 播放进度/屏幕亮度/声音大小
捏合:UIPinchGestureRecognizer *pinchGR; // 一般处理 全屏状态/视频填充状态
/*
requireGestureRecognizerToFail: A手势与其他手势建立一种关系: 如果其他手势的状态为Failed则会启用A手势动作。
(其他手势侦测失败就会调用A手势)
*/
[self.singleTap requireGestureRecognizerToFail:self.doubleTap];
核心2.手势动作&状态的判别
/*
当前手势类型
*/
typedef NS_ENUM(NSUInteger, MDHGestureType) {
MDHGestureTypeUnknown, // 位置类型
MDHGestureTypeSingleTap, // 单击
MDHGestureTypeDoubleTap, // 双击
MDHGestureTypePan, // 滑动
MDHGestureTypePinch // 捏合
};
/*
手势滑动纬度
*/
typedef NS_ENUM(NSUInteger, MDHPanDimension) {
MDHPanDimensionUnknown, // 未知方向
MDHPanDimensionVertical, // 垂直滑动
MDHPanDimensionHorizontal, // 水平滑动
};
/*
手势滑动起始区域(手指接触屏幕时在左边区域 or 右边区域)
*/
typedef NS_ENUM(NSUInteger, MDHPanLocation) {
MDHPanLocationUnknown, // 未知区域
MDHPanLocationLeft, // 左区域
MDHPanLocationRight, // 右区域
};
/*
手势滑动趋势
*/
typedef NS_ENUM(NSUInteger, MDHPanMovingDirection) {
MDHPanMovingDirectionUnkown, // 未知趋势
MDHPanMovingDirectionTop, // 向上滑动
MDHPanMovingDirectionLeft, // 向左滑动
MDHPanMovingDirectionBottom, // 向下滑动
MDHPanMovingDirectionRight, // 向右滑动
};
/*
提供目标view拒绝的手势类型
*/
typedef NS_OPTIONS(NSUInteger, MDHGestureDisableTypes) {
MDHGestureDisableTypesNone = 0,
MDHGestureDisableTypesSingleTap = 1 << 0,
MDHGestureDisableTypesDoubleTap = 1 << 1,
MDHGestureDisableTypesPan = 1 << 2,
MDHGestureDisableTypesPinch = 1 << 3,
MDHGestureDisableTypesAll = 1 << 4
};
核心3. UIGestureRecognizerDelegate
// 是否允许触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
// 是否允许同时支持多个手势,默认是不支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
// 手指触摸屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
// 手指按压屏幕后回调的方法,返回NO则不再进行手势识别,方法触发等
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceivePress:(UIPress *)press;
核心4.快进、快退时间控制
- (void)handlePan:(UIPanGestureRecognizer *)pan {
CGPoint translate = [pan translationInView:pan.view];
CGPoint velocity = [pan velocityInView:pan.view];-----拖动速率
}
核心5.移除目标View所有注册的手势
- (void)removeGesturesOnView:(UIView *)view {
if (view && [view isKindOfClass:[UIView class]]) {
__block UIView *tempView = view;
NSArray *gestureArray = view.gestureRecognizers;
[gestureArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[UIGestureRecognizer class]]) {
[tempView removeGestureRecognizer:(UIGestureRecognizer *)obj];
}
}];
}
}
demo地址:https://github.com/MadahaCEO/MDHGestureControlDemo.git