程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-UISwipeGestureRecognizerDirection問題

ios-UISwipeGestureRecognizerDirection問題

編輯:編程綜合問答
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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved