UIImageView 在 UITableViewCell 内旋转动画
UIImageView Rotate Animation inside UITableViewCell
我试图在选择 row 0
时旋转 UIImage 视图。选择该部分需要重新加载以添加另外两个单元格。这是动画无法工作的地方。 imageview 只是转换到新位置而不执行动画。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
if indexPath.row == 0 {
print(cell)
}
if displayCell {
UIView.animate(withDuration:0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
})
if indexPath.row != 0 {
swap(&indexArr[indexPath.row], &indexArr[0])
print(indexArr)
}
} else {
UIView.animate(withDuration: 0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
})
}
displayCell = !displayCell
tableView.reloadSections(IndexSet(integersIn: 0...0), with: .none)
}
第 0 行的特定单元格也需要更新内容。
这是一个 sample 项目:
您需要在动画结束后重新加载 UITableView
部分
您还需要修改您的 cellForRow 方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
switch indexPath.section {
case 0:
cell.rotateButtonImageView.isHidden = indexPath.row != 0 || indexArr.count <= 2
if(indexPath.row == 0)
{
if displayCell{
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
}else{
cell.rotateButtonImageView.transform = .identity
}
}
break
default :
cell.rotateButtonImageView.isHidden = true
break
}
cell.indexLabel.text = indexArr[indexPath.row].0
return cell
}
将此代码用于您的 didSelectRowAt
方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
if indexPath.row == 0 {
print(cell)
}
if displayCell {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
UIView.animate(withDuration: 0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
}, completion: { (finished) in
tableView.reloadSections(IndexSet(integersIn: 0...0), with: .automatic)
})
if indexPath.row != 0 {
swap(&indexArr[indexPath.row], &indexArr[0])
print(indexArr)
}
} else {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
UIView.animate(withDuration: 0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
}, completion: { (finished) in
tableView.reloadSections(IndexSet(integersIn: 0...0), with: .automatic)
})
}
displayCell = !displayCell
}
希望对您有所帮助
我试图在选择 row 0
时旋转 UIImage 视图。选择该部分需要重新加载以添加另外两个单元格。这是动画无法工作的地方。 imageview 只是转换到新位置而不执行动画。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
if indexPath.row == 0 {
print(cell)
}
if displayCell {
UIView.animate(withDuration:0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
})
if indexPath.row != 0 {
swap(&indexArr[indexPath.row], &indexArr[0])
print(indexArr)
}
} else {
UIView.animate(withDuration: 0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
})
}
displayCell = !displayCell
tableView.reloadSections(IndexSet(integersIn: 0...0), with: .none)
}
第 0 行的特定单元格也需要更新内容。 这是一个 sample 项目:
您需要在动画结束后重新加载 UITableView
部分
您还需要修改您的 cellForRow 方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
switch indexPath.section {
case 0:
cell.rotateButtonImageView.isHidden = indexPath.row != 0 || indexArr.count <= 2
if(indexPath.row == 0)
{
if displayCell{
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
}else{
cell.rotateButtonImageView.transform = .identity
}
}
break
default :
cell.rotateButtonImageView.isHidden = true
break
}
cell.indexLabel.text = indexArr[indexPath.row].0
return cell
}
将此代码用于您的 didSelectRowAt
方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
if indexPath.row == 0 {
print(cell)
}
if displayCell {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
UIView.animate(withDuration: 0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
}, completion: { (finished) in
tableView.reloadSections(IndexSet(integersIn: 0...0), with: .automatic)
})
if indexPath.row != 0 {
swap(&indexArr[indexPath.row], &indexArr[0])
print(indexArr)
}
} else {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: CGFloat(0))
UIView.animate(withDuration: 0.3, animations: {
cell.rotateButtonImageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)
}, completion: { (finished) in
tableView.reloadSections(IndexSet(integersIn: 0...0), with: .automatic)
})
}
displayCell = !displayCell
}
希望对您有所帮助