1. 程式人生 > >iOS手勢之UISwipeGestureRecognizer的正確用法

iOS手勢之UISwipeGestureRecognizer的正確用法

今天,我在用到UISwipeGestureRecognizer滑動控制器的時候,發現無論往那個方向滑動,

swipe.direction的值都是3.

下面是我以前的程式碼:

UISwipeGestureRecognizer *swiperight=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swichController:)];

//預設是UISwipeGestureRecognizerDirectionRight

        swiperight.direction=UISwipeGestureRecognizerDirectionRight|

UISwipeGestureRecognizerDirectionLeft

; 

        [self.collectionaddGestureRecognizer:swiperight];

他是錯誤的。

正確的用法是,要建立倆個輕拍手勢,分別設定他的方向才可以。

//新增輕掃手勢

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swichController:)];

//預設是UISwipeGestureRecognizerDirectionRight

        swipe.direction=

UISwipeGestureRecognizerDirectionLeft;

        [self.collectionaddGestureRecognizer:swipe];

UISwipeGestureRecognizer *swiperight=[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swichController:)];

//預設是UISwipeGestureRecognizerDirectionRight

        swiperight.direction=UISwipeGestureRecognizerDirectionRight;

        [self.collectionaddGestureRecognizer:swiperight];