请记住,tvOS 没有触摸屏幕的“触摸”概念。
处理“点击”的官方方法是使用 UITapGestureRecognizer。那就是当项目处于焦点状态时用户点击/单击遥控器时。
以下是我使用 UICollectionView 的方式:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MovieCell", forIndexPath: indexPath) as? MovieCell {
let movie = movies[indexPath.row]
cell.configureCell(movie)
if cell.gestureRecognizers?.count == nil {
let tap = UITapGestureRecognizer(target: self, action: "tapped:")
tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
cell.addGestureRecognizer(tap)
}
return cell
} else {
return MovieCell()
}
}
func tapped(gesture: UITapGestureRecognizer) {
if let cell = gesture.view as? MovieCell {
//Load the next view controller and pass in the movie
print("Tap detected...")
}
}
您可以从处理函数中传入的 UITapGestureRecognizer 中获取点击的位置。
另请参阅 Apple TV 上的本教程:
https ://www.youtube.com/watch?v=XmLdEcq-QNI