UITableViewCell

时间:2016-04-07 12:08:58

标签: ios swift core-animation uiviewanimation uianimation

我正在尝试在选择包含它的单元格时将UILabel旋转180°。我已经尝试在图层上设置变换,我尝试过CABasicAnimation,并且没有运气的UIView.animateWithDuration。

过去任何人都有这方面的经验吗?

我无法调用reloadData,因为标签位于手风琴单元格中,并且选择会导致手风琴打开/关闭。 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
        }
    }
}

2 个答案:

答案 0 :(得分:2)

在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"];

答案 1 :(得分:0)

你们发现了这个问题:

Xcode自动建议使用方法tableView(tableView: tableView, cellForRowAtIndexPath: indexPath)代替tableView.cellForRowAtIndexPath(indexPath: indexPath),这会导致新单元格出列,从而覆盖动画。