在 UITableViewCell 中旋转标签
Rotating Label inside of UITableViewCell
我试图在包含 UILabel 的单元格被选中时将其旋转 180°。我试过在图层上设置变换,试过 CABasicAnimation 和 UIView.animateWithDuration,但没有成功。
过去有人有过这方面的经验吗?
我无法调用 reloadData,因为标签位于手风琴单元格内,并且选择导致手风琴 open/close。 reloadData 覆盖 beginUpdates/endUpdates 中的动画。
这是我尝试过的方法:
func rotate180Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI)
rotateAnimation.duration = duration
if let delegate: AnyObject = completionDelegate {
rotateAnimation.delegate = delegate
}
self.layer.addAnimation(rotateAnimation, forKey: nil)
}
还有这个:
UIView.animateWithDuration(0.1) {
cell.chevronLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
}
这是我的细胞模型,如果相关的话:
class SideMenuAccountTypeCell: UITableViewCell {
@IBOutlet weak var accountLabel: UILabel!
@IBOutlet weak var abbreviationLabel: UILabel!
@IBOutlet weak var circleView: CircleView!
@IBOutlet weak var chevronLabel: UILabel!
private (set) var items = [Item]()
class Item {
var isHidden: Bool
var value: AnyObject
init(_ hidden: Bool = true, value: AnyObject) {
self.isHidden = hidden
self.value = value
}
}
class HeaderItem: Item {
init (value: AnyObject) {
super.init(false, value: value)
}
}
func append(item: Item) {
self.items.append(item)
}
func removeAll() {
self.items.removeAll()
}
func expand(headerIndex: Int) {
self.toggleVisible(headerIndex, isHidden: false)
}
func collapse(headerIndex: Int) {
self.toggleVisible(headerIndex, isHidden: true)
}
private func toggleVisible(headerIndex: Int, isHidden: Bool) {
var header = headerIndex
header += 1
while header < self.items.count && !(self.items[header] is HeaderItem) {
self.items[header].isHidden = isHidden
header += 1
}
}
}
在你的 didSelectRowAtIndexPath 中试试这个
SideMenuAccountTypeCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//transform to flips on y-axis
CATransform3D tfm = CATransform3DMakeRotation(M_PI, 0.0f, -1.0f, 0.0f);
//apply the transform before the animation so that the label remains in the same state on completion
cell.chevronLabel.layer.transform = tfm;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.5f;
animation.beginTime = 0.0f;
//apply a little perspective
tfm.34 = 0.001f
tfm.m14 = -0.001f;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D:tfm];
[cell.chevronLabel.layer addAnimation:animation forKey:@"rotateTheLabel"];
你们都发现了问题:
Xcode 自动建议将方法 tableView(tableView: tableView, cellForRowAtIndexPath: indexPath)
替换为 tableView.cellForRowAtIndexPath(indexPath: indexPath)
,这会导致新单元格出队,从而覆盖动画。
我试图在包含 UILabel 的单元格被选中时将其旋转 180°。我试过在图层上设置变换,试过 CABasicAnimation 和 UIView.animateWithDuration,但没有成功。
过去有人有过这方面的经验吗?
我无法调用 reloadData,因为标签位于手风琴单元格内,并且选择导致手风琴 open/close。 reloadData 覆盖 beginUpdates/endUpdates 中的动画。
这是我尝试过的方法:
func rotate180Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) {
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = CGFloat(M_PI)
rotateAnimation.duration = duration
if let delegate: AnyObject = completionDelegate {
rotateAnimation.delegate = delegate
}
self.layer.addAnimation(rotateAnimation, forKey: nil)
}
还有这个:
UIView.animateWithDuration(0.1) {
cell.chevronLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))
}
这是我的细胞模型,如果相关的话:
class SideMenuAccountTypeCell: UITableViewCell {
@IBOutlet weak var accountLabel: UILabel!
@IBOutlet weak var abbreviationLabel: UILabel!
@IBOutlet weak var circleView: CircleView!
@IBOutlet weak var chevronLabel: UILabel!
private (set) var items = [Item]()
class Item {
var isHidden: Bool
var value: AnyObject
init(_ hidden: Bool = true, value: AnyObject) {
self.isHidden = hidden
self.value = value
}
}
class HeaderItem: Item {
init (value: AnyObject) {
super.init(false, value: value)
}
}
func append(item: Item) {
self.items.append(item)
}
func removeAll() {
self.items.removeAll()
}
func expand(headerIndex: Int) {
self.toggleVisible(headerIndex, isHidden: false)
}
func collapse(headerIndex: Int) {
self.toggleVisible(headerIndex, isHidden: true)
}
private func toggleVisible(headerIndex: Int, isHidden: Bool) {
var header = headerIndex
header += 1
while header < self.items.count && !(self.items[header] is HeaderItem) {
self.items[header].isHidden = isHidden
header += 1
}
}
}
在你的 didSelectRowAtIndexPath 中试试这个
SideMenuAccountTypeCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//transform to flips on y-axis
CATransform3D tfm = CATransform3DMakeRotation(M_PI, 0.0f, -1.0f, 0.0f);
//apply the transform before the animation so that the label remains in the same state on completion
cell.chevronLabel.layer.transform = tfm;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.5f;
animation.beginTime = 0.0f;
//apply a little perspective
tfm.34 = 0.001f
tfm.m14 = -0.001f;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D:tfm];
[cell.chevronLabel.layer addAnimation:animation forKey:@"rotateTheLabel"];
你们都发现了问题:
Xcode 自动建议将方法 tableView(tableView: tableView, cellForRowAtIndexPath: indexPath)
替换为 tableView.cellForRowAtIndexPath(indexPath: indexPath)
,这会导致新单元格出队,从而覆盖动画。