2015-08-28 63 views
-1

視圖創建:的UIView GestureRecognizer不工作

var myView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 40)) 
myView.addSubview(myLabel) // some label 
myView.backgroundColor = UIColor.whiteColor() 
self.view.addSubview(myView) 

var mySwipeGesture = UISwipeGestureRecognizer(target: self, action: "userDidSwipe:") 
myView.userInteractionEnabled = true 
myView.addGestureRecognizer(mySwipeGesture) 

調用的方法:

func userDidSwipe(sender:UISwipeGestureRecognizer) { 
    if sender.direction == UISwipeGestureRecognizerDirection.Right { 
     switchToMain() 
    } 
} 

這不是工作,該方法userDidSwipe沒有被調用。 奇怪的是,相同的手勢識別器mySwipeGesture被添加到另一個UIView(包含許多子視圖),並完美的作品!

爲什麼它不適用於myView?我應該檢查什麼?

+1

請顯示方法userDidSwipe。 –

+0

它被添加到視圖的類型是什麼? – divergio

+0

一個簡單的UIView。 – Cherif

回答

0

您要添加另一種姿態,而不是mySwipeGesturemyView

更換你行

myView.addGestureRecognizer(swipeGesture) 

myView.addGestureRecognizer(mySwipeGesture) 
+1

我正在添加正確的手勢識別器。只是一個錯誤複製我的代碼在問題中。我糾正了它。抱歉。 – Cherif

1

通常這個工作。可能存在以下問題:

1)檢查myView的userInteractionEnabled屬性。

2)檢查myView的框架(也許你在不同的框架上滑動)。

3)myView上可能有另一個阻止手勢的視圖。