经过一番查找资料和尝试,问题得到解决,方法如下:
给UIPanGestureRecognizer添加代理(UIGestureRecognizerDelegate),UIGestureRecognizerDelegate中有一个方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch;
在进入手势识别事件之前,先进入该方法,对那些不需要响应手势事件的事件(如拖动UISlider),可以通过返回一个NO型布尔值不响应手势识别;而对于需要响应手势识别的情况,只需要返回一个YES就可以。
下面举例说明:
首先添加手势识别,并给手势识别指定代理
⋯⋯
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(handlePanGesture:)];
[panRecognizersetMaximumNumberOfTouches:1];
[panRecognizer setDelaysTouchesBegan:TRUE];
[panRecognizer setDelaysTouchesEnded:TRUE];
[panRecognizersetCancelsTouchesInView:TRUE];
//为手势识别器设置代理
panRecognizer.delegate =self;
[self.viewaddGestureRecognizer:panRecognizer];
[panRecognizer release];
⋯⋯
然后,实现UIGestureRecognizerDelegate的方法:
// 当拖动UISlider时会被误认为是手势,所以在这个判断一下
-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
if([touch.viewisKindOfClass:[UISlider class]])
returnNO;
else
returnYES;
}