2015-10-05 27 views
2

背景

我試圖做一個UICollectionViewCell更像一個按鈕(即:不要觸摸動畫暗淡)。我有一個想法,就是用UIButton填充單元格。然後該按鈕將處理被輕拍的視覺效果。但之後我需要將觸摸事件傳遞給父級(UICollectionViewCell),以便按鈕不會消耗事件。這樣我可以通過收集視圖的didSelectItemAtPath來處理它。下面的問題是我想弄清楚如何傳遞事件。如何發送觸摸事件nextResponder斯威夫特

我的問題

我碰到this Objective-C answer來傳遞觸摸事件上:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [super touchesBegan:touches withEvent:event]; 
    [self.nextResponder touchesBegan:touches withEvent:event]; 
} 

不過,我卡住了,當我試圖將其轉換爲斯威夫特:

class MyButton: UIButton { 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     super.touchesBegan(touches, withEvent: event) 
     self.nextResponder() // ??? 
    } 
} 

nextResponder方法不接受任何參數,那麼如何傳遞觸摸事件?

我無法使用相關的SO問題(herehere)來幫助我弄清楚這一點。

回答

2

nextResponder方法返回一個可選UIResponder?,所以你可以簡單地調用touchesBegan返回的對象:

self.nextResponder()?.touchesBegan(touches, withEvent: event) 
+1

這解決了我問的問題。不幸的是,我的'UICollectionViewCell'仍然沒有收到觸摸事件。但是,除非你知道問題可能出在哪邊,否則這可能需要一個新的問題。 – Suragch