簡體   English   中英

在iOS中的UITableviewCell子視圖中觸摸Scrollview的事件

[英]Touch events of Scrollview in subview of UITableviewCell in iOS

我試過通過互聯網搜索這個問題,但無法提出解決我的問題的解決方案。

我做的一個子類UITableViewCell中,我加入UIScrollView作為一個子視圖,現在我想滾動手勢由scrolView被傾聽並單擊手勢被傾聽UItableView
使用這種布局也是對抗Apple的HIG

這是一個老問題,但由於我沒有找到一個好的答案,我想我會提供一個。 此方法捕獲UIScrollView上的tap事件(包含在'表格單元格內)和直接在UITableView單元格上的tap事件,並將它們傳遞給UITableViewDelegate。 它還允許您滾動UIScrollView內容而不傳遞滾動事件。 不需要任何子類。

  1. 在ViewDidLoad中,在UITableView上實例化UITapGestureRecognizer
 // Capture taps on scrollView fields and pass along to tableView let tap = UITapGestureRecognizer(target: self, action: "tapIntercept:") tableView.addGestureRecognizer(tap) 
  1. 為上面的選擇器創建一個回調函數
 // Get the location of the table cell beneath the scrollView and pass the event off to the tableView func tapIntercept(recognizer: UIGestureRecognizer) { let point = recognizer.locationInView(tableView) if let indexPath = tableView.indexPathForRowAtPoint(point) { tableView(tableView, didSelectRowAtIndexPath: indexPath) } } 

如果滾動視圖是表視圖的子視圖,則可以捕獲手勢並將其發送到其父視圖,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM