当我使用 tapGesture 时,我希望 likeButton 从 View 外部飞入,限制为 2。当我点击时,likeButton 会出现在我需要的位置,但没有动画可以将其到达那里。在动画方面还有很多东西需要学习。有什么帮助,谢谢!!约束当前设置为刚好在 View 外部 -80。
@objc func handleTapGesture(gestureReconizer: UITapGestureRecognizer) {
let p = gestureReconizer.location(in: messageTableView)
let indexPath = messageTableView.indexPathForRow(at: p)
let cellAnimate = messageTableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath!) as! CustomMessageCell
UIView.animate(withDuration: 0.5) {
cellAnimate.likeButtonContraint.constant = 2
cellAnimate.layoutIfNeeded()
}
}
最佳答案
而不是
let cellAnimate = messageTableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath!) as! CustomMessageCell
做
let cellAnimate = messageTableView.cellForRow(at:indexPath!) as! CustomMessageCell
由于 dequeueReusableCell
返回另一个单元格以供重用,而不是您期望的位于您提供的 indexPath 处的单元格
//
let cellAnimate = messageTableView.cellForRow(at:indexPath!) as! CustomMessageCell
cellAnimate.likeButtonContraint.constant = 2
UIView.animate(withDuration: 0.5) {
cellAnimate.layoutIfNeeded()
}
关于swift - tableView 中的约束动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51564330/