2017-08-16 98 views
0

你好,我想动画一个CollectionView。它需要改变位置x和他的宽度。动画集合视图swift

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

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

回答

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通话可以帮助你。 您还在使用自定义流布局

0
  1. 创建一个简单的自定义流布局。
  2. 将此流布局类指定到故事板中的集合视图。

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

    而且比框架动画约束更好。要做到这一点:

  3. 使宽度和主要约束的出口。
  4. 在UIView动画块中更改它们的常量。
  5. 不要忘记在动画块的结尾处调用self.layoutIfNeeded()。
+0

感谢您的帮助,但我有一个问题,现在我有不好的影响时,动画开始。 单元格内的图像变宽和变窄。 –

+0

@ jimmy-gonçalves你能提供一些关于你如何建立细胞的更多信息吗?细胞大小如何变化? –

+0

我把我的实际代码展示给你看,我的单元格实际上是用UICollectionFlowLayout中的方法设置的,它的大小为item的大小。 –

0

在视图控制器,如果使用的是故事板,连接(在我的例子collectionView)使用IBOutlet中,(在我的例子collectionViewWidth)集合视图宽度约束作为IBOutlet中和UICollectionView作为IBOutlet中UICollectionviewFlowLayoutcollectionViewFlowLayout在我的例子) 。可以用下面的同时改变它的集合视图帧和细胞:

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) 
    } 
0

我现在的问题是过渡期间的一个不好的影响。

小区都设置这样的:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
{ 
    return CGSize(width: collectionView.frame.width, height: collectionView.frame.height) 
} 

而改变的代码的CollectionView领先的约束:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
{ 
    UIView.animate(withDuration: 1, delay: 0, options: .beginFromCurrentState, animations: { 
      collectionView.collectionViewLayout.invalidateLayout() 
      self.allViewCVLeadingConstraint.constant = 1000 
      self.layoutIfNeeded() 
     }, completion: nil) 
}