动画集合视图swift

时间:2017-08-16 13:49:01

标签: swift uicollectionview autoresize

您好我正在尝试为CollectionView设置动画。它需要改变位置x和他的宽度。

UIView.animate(withDuration: 1, delay: 0, options: .beginFromCurrentState, animations: {
    collectionView.frame = CGRect(x: 500, y: 0, width: 1000, height: 700)
})

但问题是细胞中的图像没有调整大小,我认为细胞也是如此。 你有解决方案吗?

3 个答案:

答案 0 :(得分:0)

UICollectionViewCell大小独立于parrent UICollectionView的框架。

也许你可以实际放大"放大"在图层上

    var transformation = CGAffineTransform.identity
    let translate = CGAffineTransform(translationX: 2, y: 2)
    let scale = CGAffineTransform(scaleX: 2, y: 2)
    transformation = scale.concatenating(translate)
    UIView.animate(withDuration: 0.15, animations: {
        self.collectionView.transform = transformation
    })

也许 invalidateLayout 电话可以帮到你。 你也在使用customflowlayout

答案 1 :(得分:0)

  1. 创建一个简单的自定义流程布局。
  2. 将此流程布局类分配给情节提要中的集合视图。

    class CustomFlowLayout: UICollectionViewFlowLayout {
        override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
           return true
        }
    }
    

    动画约束比动画更好。为此:

  3. 制作宽度和前导约束的出口。
  4. 在UIView动画块中更改它们的常量。
  5. 不要忘记在动画块结束时调用self.layoutIfNeeded()。

答案 2 :(得分:0)

在视图控制器中,如果您使用的是故事板,请使用IBOutlet,集合视图宽度约束(在我的示例中为UICollectionviewFlowLayout)作为IBOutlet连接collectionViewFlowLayout(我的示例中为collectionViewWidthUICollectionView作为IBOutlet(我的示例中为collectionView)。您可以使用以下内容同时更改集合视图框架和单元格:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5, execute: {
  self.width = 400
  self.height = 400
  self.collectionView.performBatchUpdates({
    self.collectionViewFlowLayout.invalidateLayout()
    self.collectionView.setCollectionViewLayout(self.collectionViewFlowLayout, animated: true)
  }, completion: { _ in })
})

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5, execute: {
  self.collectionViewWidth.constant = 50
  UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut,
                             animations: self.view.layoutIfNeeded, completion: nil)
})

例如,您需要将widthheight var声明为类属性,初始值为100。然后在上面的代码中进行更改。由于以下实现,它成为可能。请注意,这需要您采用协议UICollectionViewDelegateFlowLayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: width, height: height)
  }
相关问题