Ios 为uicollectionviewcell设置动画,动画期间文本对齐偏离中心
我正在设置一个Ios 为uicollectionviewcell设置动画,动画期间文本对齐偏离中心,ios,swift,uicollectionviewcell,animatewithduration,Ios,Swift,Uicollectionviewcell,Animatewithduration,我正在设置一个UICollectionViewCell的动画,以在点击时缩小。动画可以工作,但是在我设定的持续时间(本例中为0.3秒)内,文本似乎向左移动,动画朝中心移动。我尝试了动画程序的一些变体,但情况仍然存在 以下是文本问题的一个示例: 以及代码实现: func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) { var cur
UICollectionViewCell
的动画,以在点击时缩小。动画可以工作,但是在我设定的持续时间(本例中为0.3秒)内,文本似乎向左移动,动画朝中心移动。我尝试了动画程序的一些变体,但情况仍然存在
以下是文本问题的一个示例:
以及代码实现:
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
var currentCell = collectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCell!
UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.AllowUserInteraction, animations: {
currentCell.backgroundColor = self.categoryArray[indexPath.row].catColor
currentCell.frame.inset(dx: 5, dy: 5)
let imageView: UIImageView = currentCell.subviews[0] as UIImageView
let imageSize = currentCell.frame.height * 0.45
imageView.frame = CGRect(x: (currentCell.frame.width / 2) - (imageSize / 2), y:currentCell.frame.height * 0.15, width: imageSize, height: imageSize)
let textLabel:UILabel! = currentCell.subviews[2] as UILabel
textLabel.frame = CGRect(x: 0, y: currentCell.frame.height * 0.30, width: currentCell.frame.width, height: currentCell.frame.height)
let bottomBorder: UIView = currentCell.subviews[1] as UIView
bottomBorder.frame = CGRectMake(0, currentCell.frame.height - 1.0, currentCell.frame.width, 5)
}, completion: nil)
}
理想情况下,我希望文本的大小和保持居中的整个时间,而不是“浮动”到左边,然后向中心移动
…这么多笑脸…我认为使用CGAffineTransformMakeScale()更改单元格大小最有效。据我所知,没有其他方法可以以动画方式缩放标签中的文本
override func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
var currentCell = collectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCell!
UIView.animateWithDuration(0.3, animations: { () -> Void in
currentCell.contentView.backgroundColor = self.categoryArray[indexPath.row].catColor
currentCell.transform = CGAffineTransformMakeScale(0.8, 0.8)
})
}
哦,天哪,
CGAffineTransformMakeScale
简直不可思议!谢谢@是的,如果你试图用多个子视图来缩放整个视图,事情会变得容易得多。