2015-12-03 54 views
0

我有一個UITableView用的UITableViewCell這樣的:輕按通過UIScrollView的疊加在一個UITableViewCell一個UIButton不會觸發按鈕的目標

enter image description here

我添加了一個UITapGestureRecognizer(我們稱之爲目標targetCell)上的當用戶點擊一個單元格時管理tableView。在Button上,我們稱目標爲targetButton

在單元格頂部,我添加了一個UIScrollView作爲覆蓋。

我想這樣做,當在單元格/滾動視圖上有任何位置的平移/滑動時,則會檢測到平移。

但是,如果在button上有一個水龍頭,則觸發targerButton,如果它在外部targetCell被觸發。

到目前爲止,只有targetCell被觸發,位於button區域的內部或外部。如果我刪除UIScrollView,它工作正常。

所以起初我想過重寫func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool。問題是,在這個階段,它並沒有在水龍頭和水龍頭之間產生差異,所以它不能解決我的問題。

然後我想到了覆蓋func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)類似如下:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if let location = touches.first?.locationInView(tableView) { 

     var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height) 
     buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY) 

     let toNextResponder = buttonFrame.contains(location) 

     if toNextResponder { 
      self.nextResponder()?.touchesBegan(touches, withEvent: event) 
     } else { 
      super.touchesBegan(touches, withEvent: event) 
     } 
    } else { 
     super.touchesBegan(touches, withEvent: event) 
    } 
} 

它沒有工作。然後我意識到,self.nextResponder()?UITableViewCellContentView,所以我想也許,直接撥打touchesBegan(touches, withEvent: event)button。所以我改變touchesBegan這樣:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if let location = touches.first?.locationInView(tableView) { 

     var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height) 
     buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY) 

     let toNextResponder = buttonFrame.contains(location) 

     if toNextResponder { 
      if let cellContentView = self.nextResponder() as? UIView { 
       let button = findButtonInView(cellContentView) 
       button?.touchesBegan(touches, withEvent: event) 
      } 
     } else { 
      super.touchesBegan(touches, withEvent: event) 
     } 
    } else { 
     super.touchesBegan(touches, withEvent: event) 
    } 
} 

private func findButtonInView(view: UIView) -> UIButton? { 
    if view is UIButton { 
     return view as? UIButton 
    } else { 
     var button: UIButton? 
     for subview in view.subviews { 
      button = findButtonInView(subview) 
      if let _ = button { 
       return button 
      } 
     } 

     return button 
    } 
} 

而且......這是行不通的,只有targetCell被觸發,你是否在button或沒有自來水。我注意到一些事情,當我打電話給chartButton?.touchInside時,我得到了false

我做錯了什麼?或者我應該做什麼?

回答

0

嗯,我找到了一個解決方案,但我不太喜歡它,因爲它看起來更像是一個黑客而不是正確的解決方案。

時輕拍姿態是按鈕的區域內公認的我基本上執行按鈕的目標:

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool { 
    if gestureRecognizer is UITapGestureRecognizer { 
     let location = (gestureRecognizer as! UITapGestureRecognizer).locationInView(tableView) 
     var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height) 
     buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY) 

     if buttonFrame.contains(location) { 
      button.sendActionsForControlEvents(.TouchUpInside) 
      return false 
     } 
    } 

    return super.gestureRecognizerShouldBegin(gestureRecognizer) 
} 

如果任何人有一個更好的辦法還是認爲我的方式錯了,請讓我知道。

相關問題