2

I'm trying to create an animation where when the user drags a UIView then release it, the UIView return to it's initial position. Problems is, I have CAShapeLayer connected to that UIView that I want to stay connected while the UIView return to it's place. CAShapeLayer uses CABasicAnimation and the UIView uses UIView.animate.

How can I get the same timing between the two if I want the same EaseOut deceleration? Right now both animation are way off...

        let destinations = self.myAspectDestination[self.viewToDrag.accessibilityIdentifier!] ?? []
        for destination in destinations {
            if let link: CAShapeLayer = self.myAspects["\(self.viewToDrag.accessibilityIdentifier!):\(destination)"] {
                let newPath = UIBezierPath()
                newPath.move(to: (self.myPlanets[destination]?.center)!)
                newPath.addLine(to: self.originalLocation)
                let animation = CABasicAnimation(keyPath: "path")
                animation.duration = 1
                animation.isRemovedOnCompletion = true
                animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
                animation.fillMode = kCAFillModeBoth // keep to value after finishing
                animation.fromValue = link.path
                animation.toValue = newPath.cgPath
                animation.timingFunction = CAMediaTimingFunction(name: "easeInEaseOut")
                link.path = newPath.cgPath
                link.add(animation, forKey: animation.keyPath)
            }
        }

        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.viewToDrag.center = self.originalLocation
        }) { (result) in
            self.viewToDrag = nil
        }
Jaythaking
  • 2,200
  • 4
  • 25
  • 67
  • Possibly helpful (duplicate?): [How do you animate the sublayers of the layer of a UIView during a UIView animation?](https://stackoverflow.com/questions/24670269/how-do-you-animate-the-sublayers-of-the-layer-of-a-uiview-during-a-uiview-animat). – Martin R Aug 20 '18 at 14:28
  • 1
    My CAShapeLayers aren't sublayer of the animated view... not the same thing here... – Jaythaking Aug 20 '18 at 14:44

0 Answers0