uicollectionviewuibuttonuicollectionviewcellnscollectionview

change an image of a collection view after some particular time


I have an image inside a collection view cell. I am using a button tag to change its image in tap. the image is changing on the tap but the thing I need is I want to change the image of the next cell after some particular time like if I change the image now then the image of the next cell can be change after 20 to 30 seconds here is my code:

@objc func changeImage(sender: UIButton) {
        let buttonPosition = sender.convert(CGPoint.zero, to: self.tasksCollectionView)
        let index = self.tasksCollectionView.indexPathForItem(at: buttonPosition)
        let cell = self.tasksCollectionView.cellForItem(at: index ?? IndexPath()) as? TasksDetailsCollectionViewCell

cell?.backImage.image = UIImage(named: "greenCellImg")

Solution

  • I got the solution for this. On tap, I am saving the time on tap in User Defaults and then comparing that time with current time. If the time gap between these two times is 20 or 30 (required), then changing the image.