animation in collection view not kicking in

Discussion in 'iOS Programming' started by grandM, Jan 23, 2016.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    So I'm panning cells in a collection view. The cells are wiggling. I'm using following code to re-order the cells in my dataSet. Everything works out. The only problem I have is that the panned cell sometimes stops wiggling. I noticed this happens when I pan too fast, just throwing it to another place. I looked on the internet. Some people advise using invalidatelayout(). Unfortunately this isn't helping. Does anyone know what could help? Tx.
    Code:
     
    func panningMediaInMediaCollectionView(gesture: UIPanGestureRecognizer) {
          
            switch(gesture.state){
    
            case .Began:
    
                guard let selectedIndexPath = self.mediaCollectionView.indexPathForItemAtPoint(gesture.locationInView(self.mediaCollectionView)) else {break}
    
                self.mediaCollectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
    
    
            case .Changed:
    
                self.mediaCollectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(self.mediaCollectionView))
    
    
            case .Cancelled:
    
                self.mediaCollectionView.cancelInteractiveMovement()
    
    
            case .Ended:
    
                self.mediaCollectionView.endInteractiveMovement()
    
                self.mediaCollectionView.collectionViewLayout.invalidateLayout()
    
    
            default:
    
                self.mediaCollectionView.cancelInteractiveMovement()
    
            }
    
        }
    
     
  2. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #2
    It would be helpful to see what the functions performing the animations are actually doing, can you post those functions? Are you using Core Animation?
     
  3. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #3
    I'm no expert in animation. I barely know the basics about it. I'm using following code:
    Code:
    func animateZoomCell() {
    
            UIView.animateKeyframesWithDuration(0.1, delay: 0, options: UIViewKeyframeAnimationOptions.AllowUserInteraction, animations: { () -> Voidin
    
                self.transform = CGAffineTransformMakeScale(1.1, 1.1)
    
                }) { (completed) -> Voidin
    
                    self.transform = CGAffineTransformMakeScale(1.0, 1.0)
    
            }
    
        }
    
       
    
        func wobble(){
    
    
            let animationRotateDegres: CGFloat = 0.5
    
            let animationTranslateX: CGFloat = 1.0
    
            let animationTranslateY: CGFloat = 1.0
    
            let count: Int = 1
    
           
    
           
    
            let leftOrRight: CGFloat = (count % 2 == 0 ? 1 : -1)
    
            let rightOrLeft: CGFloat = (count % 2 == 0 ? -1 : 1)
    
            let leftWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * leftOrRight))
    
            let rightWobble: CGAffineTransform = CGAffineTransformMakeRotation(degreesToRadians(animationRotateDegres * rightOrLeft))
    
            let moveTransform: CGAffineTransform = CGAffineTransformTranslate(leftWobble, -animationTranslateX, -animationTranslateY)
    
            let conCatTransform: CGAffineTransform = CGAffineTransformConcat(leftWobble, moveTransform)
    
           
    
            transform = rightWobble // starting point
    
           
    
            UIView.animateWithDuration(0.1, delay: 0.08, options: [.AllowUserInteraction, .Repeat, .Autoreverse], animations: { () -> Voidin
    
                self.transform = conCatTransform
    
                }, completion: nil)
    
        }
    
       
    
        func stopWobbling(){
    
            self.layer.removeAllAnimations()
    
        }
    
     
  4. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #4
    If you use print statements are the animations getting called when they should? Try it with just a single cell to isolate the problem.

    If it is being called at the right time, is it being called on the main thread?

    Problems like this are why I use Core Animation for most purposes, you will definitely want to learn how to use it manually.
     

Share This Page