2017-12-02 109 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你的答案 –

相关问题