我一直在玩,当它被选中时试图为我的tableviewcell的内容设置动画,但我遇到了问题。我读过一些你无法在didSelectRowAtIndexPath中运行animatewithduration的地方,或者至少它没有动画,如果你这样做的话。我的代码似乎与下面的代码一样,视图移动但不动画。
我已尝试将逻辑合并到willDisplayCell中以监控选择但无济于事,所以如果有人能够为我找到合适的解决方案,我会很高兴,因为尽管有很多搜索,但答案看似无法获得:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath)
let cell = tableView.dequeueReusableCellWithIdentifier(currentCellDescriptor["cellIdentifier"] as! String, forIndexPath: indexPath) as! CustomCell
let indexOfTappedRow = visibleRowsPerSection[indexPath.section][indexPath.row]
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpandable"] as! Bool == true {
if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == false {
// INTERESTING BIT: Animates cell contents to Right
if currentCellDescriptor["cellIdentifier"] as! String == "CellHeader" {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 12, options: .CurveLinear, animations: {
cell.headerTitleLeftConstraint.priority = 999
cell.headerTitleRightConstraint.priority = 1
cell.layoutIfNeeded()
}, completion: nil)
}
} else if cellDescriptors[indexPath.section][indexOfTappedRow]["isExpanded"] as! Bool == true {
// MARK: Animates cell contents back to left
if currentCellDescriptor["cellIdentifier"] as! String == "CellHeader" {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 12, options: .CurveLinear, animations: {
cell.headerTitleLeftConstraint.priority = 1
cell.headerTitleRightConstraint.priority = 999
cell.layoutIfNeeded()
}, completion: nil)
}
}
答案 0 :(得分:1)
首先,请勿在此处使用dequeueReusableCellWithIdentifier
。它将使用屏幕上不可见的单元格并准备显示它。你想要的是cellForRowAtIndexPath
,它返回给定indexPath
屏幕上已有的单元格。
然后我明白你想玩2个约束并动画布局变化。为此,动画应仅包含layoutIfNeeded
:
cell.headerTitleLeftConstraint.priority = 999
cell.headerTitleRightConstraint.priority = 1
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 12, options: .CurveLinear, animations: {
cell.layoutIfNeeded()
}, completion: nil)
我还建议您将此逻辑从控制器传输到CustomCell
类。例如,使用selected
属性和setSelected(animated: Bool)
可视地处理状态更改。
答案 1 :(得分:0)
在Tanguy的帮助下,这就是我们的看法。我仍然有一些动画问题,因为桌子本身实际上并没有动画下面的下一级单元格,但我会改进代码并使其正常工作。认为这现在适合用途,所以值得发布以显示解决方案本身。谢谢!
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Adjusts constraints everytime switch is tapped
isLeftAligned = !isLeftAligned
if userHanded == "Right" {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 12, options: .CurveLinear, animations: {
self.leftHandedHeaderConstraint.priority = (self.isLeftAligned) ? 1 : 999
self.rightHandedHeaderConstraint.priority = (self.isLeftAligned) ? 999 : 1
self.layoutIfNeeded()
}, completion: nil)
} else if userHanded == "Left" {
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 12, options: .CurveLinear, animations: {
self.leftHandedHeaderConstraint.priority = (self.isLeftAligned) ? 999 : 1
self.rightHandedHeaderConstraint.priority = (self.isLeftAligned) ? 1 : 999
self.layoutIfNeeded()
}, completion: nil)
}
}
答案 2 :(得分:0)
您不需要使用Package: libc6-dbg
Status: install ok installed
Priority: optional
Section: debug
Installed-Size: 35315
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: i386
Multi-Arch: same
Source: glibc
Version: 2.30-0ubuntu2
Provides: libc-dbg
Depends: libc6 (= 2.30-0ubuntu2)
Description: GNU C Library: detached debugging symbols
This package contains the detached debugging symbols for the GNU C
library.
Homepage: https://www.gnu.org/software/libc/libc.html
Original-Maintainer: GNU Libc Maintainers <debian-glibc@lists.debian.org>
。 UIView.animate(...)
默认提供动画更新。更改约束后,可以通过调用以下方法来实现:
UITableView
创建时,不要忘记给自己的height属性设置比tableView.performBatchUpdates(nil, completion: nil)
低的优先级。否则,AutoLayout将在更新时打破约束(您可以在调试器中观察到此情况)。不应将约束的优先级设置为.required
或类似的东西,而应使用999
的静态属性来获得更具可读性的代码。
UILayoutPriority
/// Set this wherever you create your height constraint, e.g. in your custom cell's class.
heightConstraint?.priority = .defaultHigh
的完整代码如下:
didSelectRowAt