【问题标题】:Rotating Label inside of UITableViewCellUITableViewCell 内的旋转标签
【发布时间】:2016-04-07 12:08:57
【问题描述】:

我正在尝试在选择它的单元格时旋转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
        }
    }
}

【问题讨论】:

  • 请分享不起作用的代码。

标签: ios swift core-animation uiviewanimation uianimation


【解决方案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"];

【讨论】:

  • 与其他解决方案的效果相同,我试过了——也就是说,没有效果。我试过放置断点,并且函数 is 被触发。这是故事板的问题吗?
  • 一点也不...我已经使用故事板中的表格视图和原型单元格测试了上述内容。我会检查以确保您有正确的单元格引用。此外,上述的副作用是,当单元格被重用时, chevronLabel 将保持转换状态,因此您需要在重用单元格时将转换设置回来。考虑到这一点,您是否看到 reloadData 后的转换到位?还可以尝试将函数的内容移动到 didSelectRowAtIndexPath,以排除单元格引用问题。
  • 我在didSelectRowAtIndexPath 中发射它。经过进一步调查,似乎正在应用变换 ,但 UI 由于某种原因没有更新。我试过打电话reloadData,没有效果。
【解决方案2】:

大家发现问题了:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2011-05-17
    相关资源
    最近更新 更多