2012-05-30 33 views
1

我有一些麻煩與被監聽在屏幕上比劃我UIView過渡方法。設置SwipeGestureRecognizer爲UIView的過渡

發生什麼事情是,如果我做一個左刷卡或右刷卡發送一個左右刷卡信號給我的@selector方法..這意味着我無法區分這些刷卡。

這裏是我的代碼有問題。我已經嘗試了一些不同的東西,但似乎無法得到這個權利。

- (void) setupSwipeGestureRecognizer { 
     UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreen:)]; 
     swipeGesture.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight); 
     [self.view addGestureRecognizer:swipeGesture]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.title = @"Prototype"; 
    //Initalizse the swipe gestuer listener 
    [self setupSwipeGestureRecognizer]; 

    //alloc and init 
    self.detailViewA = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]]; 
    self.detailViewB = [[DetailViewControllerB alloc]initWithNibName:@"DetailViewControllerB" bundle:[NSBundle mainBundle]]; 

    // set detail View as first view 
    [self.view addSubview:self.detailViewA.view]; 

    // set up other views 
    [self.detailViewB.view setAlpha:1.0f]; 

    // Add the view controllers view as a subview 
    [self.view addSubview:self.detailViewB.view]; 

    // set these views off screen (right) 
    [self.detailViewB.view setFrame:CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)];  
} 


- (void)swipedScreen:(UISwipeGestureRecognizer*)gesture 
{ 
    if (gesture.direction = UISwipeGestureRecognizerDirectionLeft) { 
     NSLog(@"Left"); 
    } 
    if (gesture.direction = UISwipeGestureRecognizerDirectionRight){ 
     NSLog(@"Right"); 
    } 

} 
+2

我認爲你必須爲每個方向單獨識別 - 看看[爲UISwipeGestureRecognizer設置方向(http://stackoverflow.com/questions/ 3319209 /設置方向換uiswipegesturerecognizer)。 –

+0

好吧,所以我剛剛結束這樣做...不知道它是否是最好的解決方案,但它的挑戰性工作.. –

回答

3

類似問題herehere

swipedScreen:方法的參數類型爲UISwipeGestureRecognizer,即導致回調被調用的識別器。它不涉及用戶製作的任何實際手勢。在你的情況下,你將此識別器的direction屬性設置爲(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight) - 這不會改變。

您將不得不創建兩個識別器,每個方向一個。

+0

歡呼聲,它像這樣甜美。感謝那。 –

0

你還沒有比較你的條件是否正確。它應該有==作爲比較運算符。你正在使用=這會使你的狀況總是如此。

+0

hrmm ...似乎根本沒有使用比較運算符工作==它dosn't進入如果聲明。 –

+0

這是我正在與http://cl.ly/Gzaa一起工作的項目我還沒有改變if語句,但是如果你想嘗試,你可以......我已經打開的項目現在我已經改變了它們,而不是使用== –

2

試試這個代碼:

(void)swipedScreen:(UISwipeGestureRecognizer*)gesture { 
    if (gesture.direction == UISwipeGestureRecognizerDirectionLeft) { 
     NSLog(@"Left"); 
    } 
    if(gesture.direction == UISwipeGestureRecognizerDirectionRight) { 
     NSLog(@"Right"); 
    } 
} 
+0

nope輸入if語句,這不起作用。我剛剛調試過它,並且線程進入方法,但是對兩個if語句正確地反彈...但是如果它簡單地=然後它進入兩個if語句。 –

+1

請爲每個視圖創建兩個分開的swipegesture。併爲第一個方向指定方向,從右側指定第二個方向,爲兩個手勢對象使用相同的選擇器 –

+0

Ahhhh!涼!那完美的工作! :) 非常感謝。 –

1

斯威夫特3版本代碼:

func setupSwipeGestureRecognizer() { 

     //For left swipe 
     let swipeGestureLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen)) 
     swipeGestureLeft.direction = .left 
     self.view.addGestureRecognizer(swipeGestureLeft) 

     //For right swipe 
     let swipeGestureRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedScreen)) 
     swipeGestureRight.direction = .right 
     self.view.addGestureRecognizer(swipeGestureRight) 

    } 

override func viewDidLoad() { 
     super.viewDidLoad() 
    setupSwipeGestureRecognizer() 
} 

func swipedScreen(gesture: UISwipeGestureRecognizer) { 

     if gesture.direction == .left { 
      print("Left") 
     } else if gesture.direction == .right { 
      print("Right") 
     } 
    }