swift: animation with buttons

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



swift: animation with buttons



I use this code to create animation with button:


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



I have collectionView. But with my buttons my collectionView animated too. How to do that I have animation only with button?


collectionView


collectionView



This code doesn't work:


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





call layoutIfNeeded on your cell instead of self.view
– Shahzaib Qureshi
Aug 8 at 18:25





cell.layoutIfNeeded()
– Shahzaib Qureshi
Aug 8 at 18:29





@ShahzaibQureshi I get same result
– user
Aug 9 at 5:01





What exactly should the animation do? I don't see any code that is actually changing view components in your animations: block
– Rob
Aug 9 at 9:49


animations:





@Rob animation should move the buttons under screen edge. Update question.
– user
Aug 9 at 10:48




3 Answers
3


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


)



Try to subclass UICollectionView and override layoutSubviews method as below:


UICollectionView


layoutSubviews


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




Hope this helps.



I confirmed this code working.


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




I think your problem is that your constraints called 'button1', button2' and so on are hooked to your view controller's view or the collectionView.



You have to set up the constraint between the button(assumably inside the collection view cell) and the collection view cell's contentView, AND have that constraint property in the custom UICollectionViewCell class.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS