UIPanGestureRecognizer和UISwipeGestureRecognizer冲突?
2016-04-25 17:41
411 查看
当实现拖手势(UIPanGestureRecognizer)后将不能响应左右划(UISwipeGestureRecognizer)事件,事件会被拖手势栏截,所以左右划和拖手势只能选其一.
但是即需要拖手势还需要左右划手势呢?
我想到了一个简单方案.在UIPanGestureRecognizer基础上做一个轻滑手势.在一定的时间范围内滑动,视为左右划手势,反之为拖手势.
看代码~
- (void)viewDidLoad {
[superviewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]
initWithTarget:selfaction:@selector(panEvent:)];
[self.viewaddGestureRecognizer:pan];
}
#pragma mark - 创建一个定时器
- (void)createTimer:(void(^)(float indexSec))block {
_timerSource =dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,
0,dispatch_queue_create("timerSource",0));
//
时间间隔
double interval =
0.05f * NSEC_PER_SEC;
dispatch_source_set_timer(_timerSource,dispatch_time(DISPATCH_TIME_NOW,0),
interval,0/*最小误差*/);
__block float sec =0;
dispatch_source_set_event_handler(_timerSource, ^{
sec += 0.5f;
dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(@"sec = %f",sec);
block(sec);
});
});
dispatch_resume(_timerSource);
}
#pragma mark - 取消定时器
- (void)cancelTimer {
if (_timerSource) {
dispatch_source_cancel(_timerSource);
_timerSource =
nil;
}
}
- (void)panEvent:(UIPanGestureRecognizer *)recognizer {
if (!_timerSource) {
_firstTime_x = -1;
_firstTime_y = -1;
//
定时器 在0.1秒内滑动视为轻滑
[self createTimer:^(float indexSec) {
_indexSec = indexSec;
}];
}
// 获取手指拖拽的时候,平移的值
CGPoint translation = [recognizer
translationInView:recognizer.view];
// 大于0.1秒滑动时视为拖手势
if (_indexSec >1) {
NSLog(@"拖 ---> %@",NSStringFromCGPoint(translation));
myLabel.text = [NSStringstringWithFormat:@"拖->point
= %@",NSStringFromCGPoint(translation)];
if (recognizer.state ==UIGestureRecognizerStateEnded)
{
[self
cancelTimer];
_indexSec =
0;
}
} else {
//
在0.1秒滑动结束后视为左右划
if (recognizer.state ==UIGestureRecognizerStateEnded)
{
[self
cancelTimer];
CGFloat lastTime_x =
fabs(translation.x);
CGFloat lastTime_y =
fabs(translation.y);
if (lastTime_y -
_firstTime_y > 50 && lastTime_x -
_firstTime_x < 100) {
NSLog(@"上下滑无效");
myLabel.text =@"上下滑无效";
} else
if (lastTime_y - _firstTime_y <
100 && lastTime_x - _firstTime_x >
5) {
if (translation.x >0) {
NSLog(@"右");
myLabel.text =@"右滑";
} else {
NSLog(@"左");
myLabel.text =@"左滑";
}
}
_indexSec =
0;
}
if (_firstTime_x == -1) {
_firstTime_x =
fabs(translation.x);
}
if (_firstTime_y == -1) {
_firstTime_y =
fabs(translation.y);
}
}
}
Demo
有点不好的是
在拖拽的前0.1秒是不做事的
但是即需要拖手势还需要左右划手势呢?
我想到了一个简单方案.在UIPanGestureRecognizer基础上做一个轻滑手势.在一定的时间范围内滑动,视为左右划手势,反之为拖手势.
看代码~
- (void)viewDidLoad {
[superviewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]
initWithTarget:selfaction:@selector(panEvent:)];
[self.viewaddGestureRecognizer:pan];
}
#pragma mark - 创建一个定时器
- (void)createTimer:(void(^)(float indexSec))block {
_timerSource =dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,
0,dispatch_queue_create("timerSource",0));
//
时间间隔
double interval =
0.05f * NSEC_PER_SEC;
dispatch_source_set_timer(_timerSource,dispatch_time(DISPATCH_TIME_NOW,0),
interval,0/*最小误差*/);
__block float sec =0;
dispatch_source_set_event_handler(_timerSource, ^{
sec += 0.5f;
dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(@"sec = %f",sec);
block(sec);
});
});
dispatch_resume(_timerSource);
}
#pragma mark - 取消定时器
- (void)cancelTimer {
if (_timerSource) {
dispatch_source_cancel(_timerSource);
_timerSource =
nil;
}
}
- (void)panEvent:(UIPanGestureRecognizer *)recognizer {
if (!_timerSource) {
_firstTime_x = -1;
_firstTime_y = -1;
//
定时器 在0.1秒内滑动视为轻滑
[self createTimer:^(float indexSec) {
_indexSec = indexSec;
}];
}
// 获取手指拖拽的时候,平移的值
CGPoint translation = [recognizer
translationInView:recognizer.view];
// 大于0.1秒滑动时视为拖手势
if (_indexSec >1) {
NSLog(@"拖 ---> %@",NSStringFromCGPoint(translation));
myLabel.text = [NSStringstringWithFormat:@"拖->point
= %@",NSStringFromCGPoint(translation)];
if (recognizer.state ==UIGestureRecognizerStateEnded)
{
[self
cancelTimer];
_indexSec =
0;
}
} else {
//
在0.1秒滑动结束后视为左右划
if (recognizer.state ==UIGestureRecognizerStateEnded)
{
[self
cancelTimer];
CGFloat lastTime_x =
fabs(translation.x);
CGFloat lastTime_y =
fabs(translation.y);
if (lastTime_y -
_firstTime_y > 50 && lastTime_x -
_firstTime_x < 100) {
NSLog(@"上下滑无效");
myLabel.text =@"上下滑无效";
} else
if (lastTime_y - _firstTime_y <
100 && lastTime_x - _firstTime_x >
5) {
if (translation.x >0) {
NSLog(@"右");
myLabel.text =@"右滑";
} else {
NSLog(@"左");
myLabel.text =@"左滑";
}
}
_indexSec =
0;
}
if (_firstTime_x == -1) {
_firstTime_x =
fabs(translation.x);
}
if (_firstTime_y == -1) {
_firstTime_y =
fabs(translation.y);
}
}
}
Demo
有点不好的是
在拖拽的前0.1秒是不做事的
相关文章推荐
- Zoj 3940 Modulo Query
- quick动态控制动画的播放速度
- 强大的Burp Suite
- 从setTimeout到浏览器线程机制 ,实现JS线程和UI同时执行的效果
- 大坑总结(一)
- 【Vue】-(3) Vue与MVVM
- UIViewController数据刷新
- Intellij IDEA 14 自动生成 serialVersionUID
- emulator: ERROR: x86 emulation currently requires hardware ac
- 对石家庄铁道大学网站的UI分析
- NSURLRequestCachePolicy 缓存的方式
- 找不到指定版本的API,failed to find Build Tools revision 23.0.0 rc2
- <OJ_Sicily>N_Queen Problem
- LeetCode *** 304. Range Sum Query 2D - Immutable
- 浅谈Java web 中request的setAttribute()用法
- Hibernate中的query.setFirstResult(),query.setMaxResults();
- 虚拟机guest为windows7的环境下安装破解版simplify3d_3.0.2
- android Bluetooth常用AT 命令
- Android 高级UI设计笔记15:HorizontalScrollView之 实现画廊式图片浏览器
- hdu 1242 Rescue