当我滚动的tableView,我怎么能知道哪个小区触摸绿色区域
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的解决方案是好的,斯威夫特的解决方案是为我好,谢谢你这么多
回答:
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"
}
}
}
}
回答:
如何像:
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
}
}
}
回答:
在这段代码中,我使用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)
}
}
}
}
查找下面截图:
以上是 当我滚动的tableView,我怎么能知道哪个小区触摸绿色区域 的全部内容, 来源链接: utcz.com/qa/257413.html