CABasicAnimation останавливается при перезагрузке UICollectionView - PullRequest
0 голосов
/ 14 февраля 2019

Мне нужна помощь в выпуске коллекций.В моем collectionView я использую UILongPressGestureRecognizer для запуска CABasicAnimation в элементах (изображениях), но когда я удаляю элемент и перезагружаю collectionView, CABasicAnimation прерывается.Как я могу предотвратить остановку анимации?Мне нужно, чтобы анимация продолжалась до тех пор, пока пользователь не решит остановиться.

Вот некоторая информация о моем collectionView:

class SentMemesCollectionVC: UICollectionViewController {

//MARK: - PROPERTIES

var memes: [Meme]! {
    return Meme.accessMemes().memes
}

//MARK: - LIFE CYCLE

override func viewDidLoad() {
    super.viewDidLoad()

    let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
    self.collectionView.addGestureRecognizer(longPressGesture)
}

@ objc func longTap (_ жест: UILongPressGestureRecognizer) {

    switch gesture.state {
    case .began:
        print("LongPress begin")
        guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {return}
        collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
        setEditing(true, animated: true)

    default:
        collectionView.cancelInteractiveMovement()
    }

}

// MARK: - COLLECTIONVIEW DATA SOURCE


//Defini o número de itens em cada seção
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   return memes.count
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! GridMemeCell
    let meme = memes[indexPath.row]
    cell.prepareGridCell(with: meme)
    cell.delegate = self
    return cell
}

// MARK: - COLLECTIONVIEW DELEGATE
/*
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let memeDetail = storyboard?.instantiateViewController(withIdentifier: "DetailVC") as! MemeDetailVC
    let meme = memes[indexPath.row]
    memeDetail.memeSelected = meme
    Feedback.share.hapticFeedback()

    navigationController?.pushViewController(memeDetail, animated: true)
}
*/
// MARK: - DELETE ITEMS IN COLLECTVIEW


override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    Feedback.share.hapticFeedback()

    if let indexPaths = collectionView?.indexPathsForVisibleItems {
        for indexPath in indexPaths {
            if let cell = collectionView?.cellForItem(at: indexPath) as? GridMemeCell {

                if editing {
                    cell.isEditing = editing
                    cell.startAnimate()
                    doneBarButtonItem()

                } else {
                    cell.isEditing = editing
                    cell.stopAnimate()
                    addBarButtonItem()
                }
            }
        }
    }
}

}

Вот мой CollectionViewCell

class GridMemeCell: UICollectionViewCell {

//MARK: - OUTLETS

@IBOutlet weak var ivImage: UIImageView!
@IBOutlet weak var deleteMeme: UIVisualEffectView!

//MARK: - PROPERTIES

weak var delegate: GridMemeCellDelegate?

var isAnimate: Bool! = true

//MARK: - METHODS AND COMPUTED PROPERTIES


func prepareGridCell(with meme: Meme) {
    ivImage.image = meme.memeImage

    deleteMeme.layer.cornerRadius = deleteMeme.bounds.width / 2.0
    deleteMeme.layer.masksToBounds = true
    deleteMeme.isHidden = !isEditing
    deleteMeme.contentView.backgroundColor = Theme.current.subViewColor

}

func startAnimate() {
    let shakeAnimation = CABasicAnimation(keyPath: "transform.rotation")
    shakeAnimation.duration = 0.05
    shakeAnimation.repeatCount = 4
    shakeAnimation.autoreverses = true
    shakeAnimation.duration = 0.2
    shakeAnimation.repeatCount = 99999

    let startAngle: Float = (-2) * 3.14159 / 180
    let stopAngle = -startAngle

    shakeAnimation.fromValue = NSNumber(value: startAngle)
    shakeAnimation.toValue = NSNumber(value: 3 * stopAngle)
    shakeAnimation.autoreverses = true
    shakeAnimation.timeOffset = 290 * drand48()

    let layer: CALayer = self.layer
    layer.add(shakeAnimation, forKey: "animate")
    self.deleteMeme.isHidden = false
    isAnimate = true

}

func stopAnimate() {
    let layer: CALayer = self.layer
    layer.removeAnimation(forKey: "animate")
    self.deleteMeme.isHidden = true
    isAnimate = false

}


var isEditing: Bool = false {
    didSet {
        deleteMeme.isHidden = !isEditing

    }
}

@IBAction func btDeleteMeme(_ sender: Any) {
    Feedback.share.hapticFeedback()
    delegate?.deleteCell(cell: self)

}

}

И вотМоя ячейка делегата протокола:

protocol GridMemeCellDelegate: class {
func deleteCell(cell: GridMemeCell)

}

А вот мое расширение CollectionView - Delegate.

extension SentMemesCollectionVC: GridMemeCellDelegate {
func deleteCell(cell: GridMemeCell) {
    if let indexPath = collectionView.indexPath(for: cell) {

        //Apaga o Meme do Array
        Meme.accessMemes().memes.remove(at: indexPath.item)
        collectionView.reloadData()

    }
}

}

...