选择时动画UICollectionViewCell标签

时间:2016-03-28 23:18:21

标签: ios swift animation

我有一个自定义UICollectionViewCell,其中包含自定义标签。当用户点击单元格时,我希望动画显示自定义标签的外观。我已尝试保留引用变量并调用collectionView.reloadData(),但在我的cellForItemAtIndexPath中,更新不会设置动画。相反,它立即发生。如何为标签的字体和大小更改设置动画?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        self.flatPicker.reloadData()
        self.variable = indexPath.row

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell

        cell.label.text = self.array[indexPath.row]

        if indexPath.row == variable {

            UIView.animateWithDuration(2, animations: { 

                cell.label.font = UIFont.boldSystemFontOfSize(12)
                cell.label.textColor = UIColor.darkGrayColor()

            })


        } else {

            UIView.animateWithDuration(2, animations: {

                cell.label.font = UIFont.systemFontOfSize(11)
                cell.label.textColor = UIColor.lightGrayColor()

            })

        }

        return cell
    }

2 个答案:

答案 0 :(得分:0)

字体和文字颜色不可动画。

你可以做的是使用粗体/常规属性将两个标签叠加在一起,并使用.alpha

在两者之间交叉淡入淡出

答案 1 :(得分:0)

你不应该在cellForItemAtIndexPath中使用选定的单元格,而是在didSelectItemAtIndexPath中。

检索当前选定的单元格并为其设置动画

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
   let cell = collectionView.cellForItemAtIndexPath(indexPath)
   cell.label.font = UIFont.boldSystemFontOfSize(12)
   cell.label.textColor = UIColor.darkGrayColor()

}

正如大卫所说,字体和颜色不具有可动画的属性。

相关问题