我有一个tableView,当用户点击该单元格时,我希望该单元格扩展并用该颜色填充整个屏幕。目前,它确实会缩放以填满整个屏幕,但上方的单元格也会显示出来。颜色来自API,并且当用户点击“生成”按钮时,每个单元格都会更改颜色。由于API的JSON文件的结构方式,我一次只希望在屏幕上显示5种颜色。
我不知道为什么其上方的单元格不会向上移动,或者如何将单击的单元格移至屏幕顶部。我尝试过偏移已点击的单元格的坐标并将其帧大小更改为tableView tappedCell.bounds.size = CGSize(width: UIScreen.main.bounds.width, height: self.paletteTableView.bounds.height)
的高度,但是当我从顶部单击第一个或第二个单元格时,屏幕无法完全填充到底部。
[我还尝试了从UIView(在此称为colorDetailsView
)制作动画,但无法成功地使其从所点击的每个单元格的中心进行动画,仅从屏幕的中心开始。
动画在这里发生:
@objc func userTap(sender: UITapGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.ended {
let tapLocation = sender.location(in: self.paletteTableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) {
UIView.animate(withDuration: 0.5, animations: {
switch self.currentAnimation {
case 0:
tappedCell.transform = CGAffineTransform(scaleX: 1, y: 50)
//...
case 1:
tappedCell.transform = .identity
default: break
}
} )
currentAnimation += 1
if currentAnimation > 1 {
currentAnimation = 0
} } } } }
完整代码:
class PaletteController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView()
var colorPalette = [Color]()
var currentAnimation = 0
let colorDetailsView: UIView = {
let view = UIView()
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.isScrollEnabled = false
\\...
view.addSubview(colorDetailsView)
}
//Color cell expands to fill the screen of that color when user taps on the cell
@objc func userTap(sender: UITapGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.ended {
let tapLocation = sender.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) {
UIView.animate(withDuration: 0.5, animations: {
//When user taps on a cell, it is scaled to fill the screen with that color
switch self.currentAnimation {
case 0:
tappedCell.transform = CGAffineTransform(scaleX: 1, y: 50)
for i in 0..<self.colorPalette.count {
if tapIndexPath.row == i {
self.colorDetailsView.backgroundColor = UIColor(hexString: self.colorPalette[i])
}
}
case 1:
tappedCell.transform = .identity
default: break
}
} )
currentAnimation += 1
if currentAnimation > 1 {
currentAnimation = 0
}
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"ColorCell", for: indexPath)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(userTap(sender:)))
cell.addGestureRecognizer(tapGesture)
cell.isUserInteractionEnabled = true
//Assigns a new color to each row. Colors are converted from HEX to RGB
for i in 0..<colorPalette.count {
if tapIndexPath.row == i {
cell.backgroundColor = UIColor(hexString: colorPalette[i])
}
}
return cell
}
}
我知道当我将tableView.isScrollEnabled
设置为true时,单元正在缩放,如下所示。每个单元格最终应填满整个屏幕,并且不要像其他最顶部的单元格那样被其他任何单元格覆盖。理想情况下,我希望tableView不滚动,但是我不确定如果滚动被禁用,则顶部单元格是否将“向上移动”。
感谢您的任何帮助!
这些单元格位于表视图的所有子视图之后。您需要将点击的单元格放在子视图列表的前面。
我建议添加
tableView.bringSubviewToFront(tappedCell)
仅在行后
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) {
。
看看是否可行!