在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;
}