Search code examples
swiftanimationcore-animationuiviewanimation

Warning: CoreAnimation stiffness must be greater than 0, but I use viewAnimation


I don't know why, but I suddenly get this warning, what I didn't got before:

CoreAnimation: stiffness must be greater than 0.

CoreAnimation: damping must be greater than or equal to 0.

CoreAnimation: stiffness must be greater than 0.

CoreAnimation: damping must be greater than or equal to 0.

For my animation I use viewAnimation and is that not from the framework UIKit? Because damping,stiffness is from layerAnimation and that's from the framework CoreAnimation.

The problem occurs when I change my constraint.

Here is my code:

Enlarge image:

 @IBAction func posterButton(sender: AnyObject) {
        
        // Unhide poster
        poster.hidden = false
        
        // Show poster:
        for constraint in poster.superview!.constraints {
            
            if constraint.identifier == "EnlargePoster" {
                
                constraint.active = false
                
                let newConstraint = NSLayoutConstraint(item: self.poster, attribute: .Bottom, relatedBy: .Equal, toItem: self.poster.superview!, attribute: .Bottom, multiplier: 1, constant: 0)
                
                newConstraint.identifier = "EnlargePoster"
                newConstraint.active = true
                
                UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: [], animations: {
                    
                    self.view.layoutIfNeeded()
                    
                    }, completion: {_void in
                        
                        // show poster close button
                        self.hidePosterButton.hidden = false
                })
            }
        }
    }

Scale down image:

@IBAction func hidePoster(sender: AnyObject) {
        
        // hide poster:
        for constraint in poster.superview!.constraints {
            
            if constraint.identifier == "EnlargePoster" {
                
                constraint.active = false
                
                let newConstraint = NSLayoutConstraint(item: self.poster, attribute: .Bottom, relatedBy: .Equal, toItem: self.poster.superview!, attribute: .Bottom, multiplier: 1, constant: -672)
                
                newConstraint.identifier = "EnlargePoster"
                newConstraint.active = true
                
                UIView.animateWithDuration(0.6, delay: 0, usingSpringWithDamping: 0.0, initialSpringVelocity: 0.0, options: [], animations: {
                    
                    self.view.layoutIfNeeded()
                    
                    }, completion: {_void in
                        
                        // Hide poster
                        self.poster.hidden = true
                })
            }
        }

Solution

  • UIView animation is a high-level wrapper over Core Animation. Under the covers the system creates one or more CAAnimation objects in order to implement the requested animation.

    In this particular case it sounds like the damping parameter must be greater than 0, but that requirement isn't enforced by the UIKit method - it generates an error when the system maps your UIKit animation to Core Animation. The message you're getting makes it pretty clear that damping needs to be > 0, so use a value greater than zero.

    I'm not sure what stiffness is. That must be a parameter used in the underlying Core Animation call that isn't used in the UIView animation call. My guess is that error will go away when you use a > 0 damping value.