Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 滚动UICollectionView时设置NSLayoutConstraint动画_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 滚动UICollectionView时设置NSLayoutConstraint动画

Ios 滚动UICollectionView时设置NSLayoutConstraint动画,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我的UICollection上方有两个标签,在collection视图中滚动时需要设置动画。基本上给用户一种感觉,屏幕是一个大的可滚动视图 在interface builder中,我的视图如下所示: 我想出了以下代码,使它在较小的屏幕上看起来更好,但它没有给我想要的效果: func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: Un

我的UICollection上方有两个标签,在collection视图中滚动时需要设置动画。基本上给用户一种感觉,屏幕是一个大的可滚动视图

在interface builder中,我的视图如下所示:

我想出了以下代码,使它在较小的屏幕上看起来更好,但它没有给我想要的效果:

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if(velocity.y>0){
        if Device.IS_3_5_INCHES() {
            self.topConstraint.constant = -100
        }
        if Device.IS_4_INCHES() {
            self.topConstraint.constant = -70
        }
        if Device.IS_4_7_INCHES() {
            self.topConstraint.constant = 0
        }
        if Device.IS_5_5_INCHES() {
            self.topConstraint.constant = 0
        }
    }else{
        self.topConstraint.constant = 50
    }
}
func ScrollViewWillendDraging(scrollView:UIScrollView,withVelocity:CGPoint,targetContentOffset:UnsafemeutablePointer){
如果(速度y>0){
如果设备为3英寸5英寸(){
self.topConstraint.constant=-100
}
如果设备为4英寸(){
self.topConstraint.constant=-70
}
如果设备为4英寸7英寸(){
self.topConstraint.constant=0
}
如果设备为5英寸(){
self.topConstraint.constant=0
}
}否则{
self.topConstraint.constant=50
}
}
这段代码的问题是,屏幕现在“跳跃”,而不是像它是集合视图的一部分那样设置动画


这段代码的另一个问题是,一旦你开始向另一个方向滚动,它就会“跳回”。无论您的应用程序位于UIScrollView的顶部还是底部。将TopConstraint设置回50应该只在您处于顶部时才满意

以下是如何将前两个单元格设置为集合视图的宽度:

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let insets = collectionView.contentInset
let collectionViewWidth = collectionView.frame.width - (insets.left + insets.right + 1)
let collectionViewHeight = collectionView.frame.height - (insets.top + insets.bottom)

  switch indexPath.row {
    case 0, 1:

    return CGSize(width: collectionViewWidth, height: collectionViewHeight / 8)

      default:
     return CGSize(width: collectionViewWidth / 3, height: collectionViewHeight / 8)
    }
}
您可以更改高度除以多少以获得所需的比率。与默认值相同。假设您希望前2行之后的所有单元格为每行3个,您可以将collectionViewWidth/3分开


然后,您可以创建一个不同的集合视图单元格子类来生成前两个单元格,并在cellForItemAtIndexPath中检查它是哪个indexPath,并相应地设置单元格

您可以使用集合视图标题作为替代吗?为什么不让前两个单元格占据整行并将标签放在每个单元格内?我对任何事情都持开放态度。滚动时是否显示标题?我喜欢这些选项!请教我如何:)您可以先调用
layoutifneedd
然后设置约束值来设置约束动画。例子: