您的位置:首页 > 产品设计 > UI/UE

UIPanGestureRecognizer手势影响UISlider拖动的问题及解决办法 (转)

2012-08-24 00:23 357 查看
最近在使用UISlider控件控制音频播放进度时,遇到一个问题,即:UISlider所在的UIView上已经添加了 UIPanGestureRecognizer类型手势,用来处理向右拖动整个View时取消显示,但这样一来,拖动UISlider就变成了响应手势, 而无法正常使用UISlider。
经过一番查找资料和尝试,问题得到解决,方法如下:
给UIPanGestureRecognizer添加代理(UIGestureRecognizerDelegate),UIGestureRecognizerDelegate中有一个方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch;
在进入手势识别事件之前,先进入该方法,对那些不需要响应手势事件的事件(如拖动UISlider),可以通过返回一个NO型布尔值不响应手势识别;而对于需要响应手势识别的情况,只需要返回一个YES就可以。
下面举例说明:
首先添加手势识别,并给手势识别指定代理
……
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:selfaction:@selector(handlePanGesture:)];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelaysTouchesBegan:TRUE];
[panRecognizer setDelaysTouchesEnded:TRUE];
[panRecognizer setCancelsTouchesInView:TRUE];
//为手势识别器设置代理
panRecognizer.delegate = self;
[self.view addGestureRecognizer:panRecognizer];
[panRecognizer release];
……
然后,实现UIGestureRecognizerDelegate的方法:
// 当拖动UISlider时会被误认为是手势,所以在这个判断一下
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
if([touch.view isKindOfClass:[UISlider class]])
return NO;
else
return YES;
}

----------------------------------------------
uiscrollview

//手势
UIPanGestureRecognizer *panGesture =
[[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
[panGesture release];

self.loginScroll.contentSize=CGSizeMake(675, 254);

- (void)panGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

//scrollView区域滑动,之后return,跳出方法,页面不滑动。
if (loginView.hidden == NO && loginScroll.scrollEnabled == YES) {
CGPoint touchPoint = [gestureRecognizer locationInView:keyWindow];

if (touchPoint.x > 48 && touchPoint.x < 273 && touchPoint.y > 118 && touchPoint.y < 372) {
return;
}
}

if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
touchBeganPoint = [gestureRecognizer locationInView:keyWindow];
touchLastPoint = touchBeganPoint;

} else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
if (isScroll == YES) {
return;
}

CGPoint touchPoint = [gestureRecognizer locationInView:keyWindow];
CGFloat xOffSet = touchLastPoint.x - touchPoint.x;
touchLastPoint = touchPoint;

if(self.navigationController.view.frame.origin.x-xOffSet >= 0)
{
return;
}

if (tableView.scrollEnabled == NO) {
self.navigationController.view.frame = CGRectMake(self.navigationController.view.frame.origin.x-xOffSet,
self.navigationController.view.frame.origin.y,
self.navigationController.view.frame.size.width,
self.navigationController.view.frame.size.height);
return;
}

if (touchBeganPoint.x - touchPoint.x >= 10) {
tableView.scrollEnabled = NO;
loginScroll.scrollEnabled=NO;
} else if (touchPoint.y - touchBeganPoint.y >= 5 || touchBeganPoint.y - touchPoint.y >= 5) {
isScroll = YES;
}

} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

[tableView setScrollEnabled:YES];
if (isScroll == NO) {
if (touchBeganPoint.x - [gestureRecognizer locationInView:keyWindow].x > kTriggerOffSet)
[self moveToLeftSide];
else
[self restoreViewLocation];
}
isScroll = NO;
self.loginScroll.scrollEnabled=YES;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: