问题描述
- UISwipeGestureRecognizerDirection问题
-
在viewDidLoad 中设置:UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)]; [self.view addGestureRecognizer:swipeGesture]; -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe received."); UISwipeGestureRecognizerDirection temp = recognizer.direction; if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { [self backCalendarPressed:nil]; } else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) { [self nextCalendarPressed:nil]; } }
但是
recognizer.direction
总是等于3,这样的结果我没有办法判断是向左滑动还是向右滑动。
解决方案
在初始时设置确定一个初始状态.比如下一步的动作是向左滑. 设置direction
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionLeft];
只添加对向左滑的手势支持,而不是两个方向的支持.可加一个BOOL类型的变量来标识当前的滑动方向
BOOL isLeft=NO;
在处理滑动的事件处理中,根据当前方向重置direction
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
UISwipeGestureRecognizerDirection temp = recognizer.direction;
if (!isLeft)
{
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionRight];
[self backCalendarPressed:nil];
}
else
{
[swipeGesture setDirection:UISwipeGestureRecognizerDirectionLeft];
[self nextCalendarPressed:nil];
}
isLeft=!isLeft;
}
时间: 2024-09-20 11:44:35