iosswiftfacebook-pop

Animation not working when use POPDecayAnimation


I make dropping down animation for changing views. When I use POPSpringAnimation all working correctly. But when I change POPSpringAnimation to POPDecayAnimation nothing happens and in debug output I see this message:

ignoring to value on decay animation <POPDecayAnimation:0x7f9f2b5c8700; from = null; to = null; deceleration = 0.998000>

Code:

func switchViewController(from: UIViewController?, toViewController to: UIViewController?, animated: Bool) {

    if animated {
        to!.view.frame = self.view.frame
        // Not working when call trulySwitchViewController
        self.addChildViewController(to!)
        self.view.insertSubview(to!.view, belowSubview: from!.view)
        to!.didMoveToParentViewController(self)
        // Calculate new x coord
        var frame = from!.view.frame
        frame.origin.y += frame.size.height

        // POPDecayAnimation not working here
        var animation = POPDecayAnimation()
        animation.property = POPAnimatableProperty.propertyWithName(kPOPViewFrame) as POPAnimatableProperty
        animation.toValue = NSValue(CGRect:frame)
        animation.completionBlock = {
            (_, _) in
            self.trulySwitchViewController(from, toViewController: nil)
        }

        from!.view.pop_addAnimation(animation, forKey: "dropDown")
    } else {
        to?.view.frame = self.view.frame
        self.trulySwitchViewController(from, toViewController: to)
    }
}

Solution

  • For a decay animation you need to set the velocity property, this then decays to zero based on the deceleration property. So, the to property is not needed, the end state is determined based on the start position, velocity and deceleration.

    You also need to animate the layer position not the view frame. So something like this should work...

    var animation = POPDecayAnimation()
    animation.property = POPAnimatableProperty.propertyWithName(kPOPLayerPosition) as POPAnimatableProperty
    
    from!.view.layer.pop_addAnimation(animation, forKey: "dropDown")