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

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

最佳答案

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

    })
}

10-08 05:46
查看更多