Ios swift:带按钮的动画

Ios swift:带按钮的动画,ios,swift,uibutton,uiviewanimation,Ios,Swift,Uibutton,Uiviewanimation,我使用以下代码创建带有按钮的动画: func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { switch indexPath.section { case 0: self.button1.constant = self.button2.constant

我使用以下代码创建带有按钮的动画:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

    switch indexPath.section {
    case 0:
        self.button1.constant = self.button2.constant
    case 6:
        self.button3.constant = self.button4.constant
    default:
        self.button1.constant = 0
        self.button3.constant = 0
    }

    UIView.animate(withDuration: 1.0, delay: 0.0,
                   options: [], animations: {
                    self.view.layoutIfNeeded()
    })
}
我有
collectionView
。但是用我的按钮,我的
collectionView
也被激活了。如何做到这一点,我只有动画按钮

此代码不起作用:

UIView.animate(withDuration: 1.0, delay: 0.0,
               options: [], animations: {
                self.myButton.layoutIfNeeded()
                self.myButton1.layoutIfNeeded()
})

尝试子类化
UICollectionView
并覆盖
layoutSubviews
方法,如下所示:

override func layoutSubviews() {
     UIView.performWithoutAnimation {
         super.layoutSubviews()
     }
}

希望这能有所帮助。

我确认此代码有效

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

UIView.animate(withDuration: 1.0, delay: 0.0,
                       options: [], animations: {

        switch indexPath.section {
        case 0:
            self.button1.constant = self.button2.constant
            self.myButton1.layoutIfNeeded()
        case 6:
            self.button3.constant = self.button4.constant
            self.myButton3.layoutIfNeeded()
        default:
            self.button1.constant = 0
            self.button3.constant = 0

            self.myButton1.layoutIfNeeded()
            self.myButton6.layoutIfNeeded()
        }

    })
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    guard let cell = cell as? MyCollectionViewCell else { return }

    cell.yourConstraint.constant = 10 //desired value

    UIView.animate(withDuration: 0.3) {
        cell.layoutIfNeeded()
    }
}
我认为您的问题在于称为“button1”、“button2”等的约束与视图控制器的视图或collectionView挂钩


您必须在按钮(假定位于集合视图单元格内)和集合视图单元格的contentView之间设置约束,并在自定义UICollectionViewCell类中设置该约束属性。

在单元格上调用layoutIfNeeded,而不是self.viewcell.layoutIfNeeded()@ShahzaibQureshi我得到同样的结果动画应该做什么?在
动画:{}
block@Rob动画应移动屏幕边缘下的按钮。更新问题。