2017-12-02 223 views
1

The Screen In My Prototype當我滾動的tableView,我怎麼能知道哪個小區觸摸綠色區域

我的問題是基於The image in the link。因爲我的名氣是不夠的,我不能發表任何的形象在這裏

我們假設格林區域的圖像中固定
而且,我的要求是,當a cell包含GA,that cell's audioPlayer將在單元格說一句話,就像AirPod

OR,你可以把我的要求,因爲When a cell contains the GA, the text of that cell's label changes to "Touch the Green"

我的問題是我在滾動tableView的時候怎麼能得到which one(Cell)是否包含GA? 但我不能找到一種方式來獲得(有關該小區一些位置/索引信息)

任何人都可以幫我嗎?的ObjectiveC的解決方案是好的,斯威夫特的解決方案是爲我好,謝謝你這麼多

+0

ü可以分享屏幕截圖?你需要什麼確切的屏幕? –

+0

Bcos,你不能使用UITableView作爲UIPickerView。所以可能的分享屏幕。 –

+0

https://stackoverflow.com/questions/31515151/uiscrollview-scroll-by-offset這裏有一個圖像鏈接。我問這個基於這個圖像的問題,這個圖像是否使問題清晰? –

回答

1
override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    // We check cells here to set the state of whether it contains the green or not before the scrolling 
    checkCells() 
} 

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    // And we are continuously checking cells while scrolling 
    checkCells() 
} 

func checkCells() { 
    tableView.visibleCells.forEach { cell in 
     if let indexPath = tableView.indexPath(for: cell) { 
      let rect = tableView.rectForRow(at: indexPath) 
      // This is the rect in your VC's coordinate system (and not the table view's one) 
      let convertedRect = self.view.convert(rect, from: tableView) 

      if convertedRect.contains(greenArea.frame) { 
       cell.textLabel?.text = "Touch the Green" 
      } else { 
       cell.textLabel?.text = "Does not touch the Green" 
      } 
     } 
    } 
} 
+0

非常感謝你的幫助,Ruslan。您的代碼清晰易懂,而且正是我想要的。 –

0

如何像:

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    [0, 1, 2].forEach { 
     let rect = tableView.rectForRow(at: IndexPath(row: $0, section: 0)) 
     if rect.contain(GAView.frame) { 
      // play sound here 
     } 
    } 
} 
2

在這段代碼中,我使用GreenArea如在UIView中心。 Ruslan's的一些修改答案。

@IBOutlet weak var greenAreaVw: UIView! 
var contHeight : CGFloat = 0.0 
var eachRowHeight : CGFloat = 45 
var topSpaceTableView : CGFloat = 62 
var GreenAreaOriginY : CGFloat = 0.0 

// Give UITableView Edge Insets in ViewDidLoad 

contHeight = ((self.view.frame.size.height/2) - eachRowHeight/2 - topSpaceTableView) 
userTblVw.contentInset = UIEdgeInsets(top: contHeight, left: 0, bottom: contHeight, right: 0) 
userTblVw.contentOffset.y = -contHeight 
GreenAreaOriginY = greenAreaVw.frame.origin.y 

/*-------------------   -----------------------*/ 
func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 

    checkCells() 
} 

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { 

    checkCells() 
} 


func checkCells() { 

    userTblVw.visibleCells.forEach { cell in 
     if let indexPath = userTblVw.indexPathForCell(cell) { 

      let rect = userTblVw.rectForRowAtIndexPath(indexPath) 
      let convertedRect = self.userTblVw.convertRect(rect, toView: self.view) 

      if convertedRect.origin.y >= GreenAreaOriginY && convertedRect.origin.y < (GreenAreaOriginY + eachRowHeight) 
      { 
       let contFloat : CGFloat = (eachRowHeight * CGFloat(indexPath.row)) - contHeight 
       userTblVw.setContentOffset(CGPoint(x: 0, y: contFloat), animated: true) 
      } 

     } 
    } 
} 

查找下面截圖:

enter image description here

+0

非常感謝您的幫助,McDonal,您的解決方案非常好,讓tableView具有像pickerView這樣的效果,比我的要求更強大。 –

+0

你可以爲此讚不絕口嗎? –

+0

沒有足夠的聲望。當我得到足夠的,我會upvote你的答案 –

相關問題