ios – How do I delay operating a line of code till animations are completed? (DispatchGroup & DispatchQueue)

0
3


I am working in Swift 5 (Xcode 13.4.1) and I’ve bought 3 animations that ought to run after a button is pressed. One of many animations, ‘rotate’, is outlined by a perform. The rotate animation would not cease after period ends, it simply restarts. I am attempting to cease the rotation animation utilizing self.UI_Image_Name.layer.removeAllAnimations.

I can not get my code to attend till animations are completed to run the removeAllAnimations line. I have been attempting to observe this tutorial: Ready till the duty finishes

This is my present code:

@IBAction func flipButtonPress(_ sender: UIButton) {
        
//        flipResult.picture = outcomes[Int.random(in: 0...1)]
        
        
        self.animationGroup.enter()
        DispatchQueue.foremost.asyncAfter(deadline: DispatchTime.now(), execute: { () -> Void in
        //Slide coin picture to center of display
        UIView.animate(withDuration: 3, delay: 0,
                       choices: [.curveEaseIn, .curveEaseOut],
                       animations: {
                            self.coinImage.body.origin.y = 220
                       })
        //Rotate Coin picture
            UIView.animate(withDuration: 11, delay: 3,
                           choices: [.curveEaseIn, .curveEaseOut],
                           animations: {
                                self.coinImage.rotate()
            })
          //Fade away coin picture
            UIView.animate(withDuration: 5, delay: 6,
                           choices: [],
                           animations: {
                self.coinImage.alpha = 0.5
                                       
        })
            self.animationGroup.go away()
        })
        
        
        self.animationGroup.enter()
        DispatchQueue.foremost.asyncAfter(deadline: DispatchTime.now(), execute: { () -> Void in
            self.coinImage.layer.removeAllAnimations()
        })
        self.animationGroup.go away()
        
    }
    
}

extension UIImageView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "remodel.rotation.z")
        rotation.toValue = NSNumber(worth: (Double.pi * 2) * 22)
        rotation.period = 11
        rotation.isCumulative = true
        rotation.repeatCount = Float.greatestFiniteMagnitude
        rotation.timingFunction = CAMediaTimingFunction(title: CAMediaTimingFunctionName.easeInEaseOut)
        rotation.beginTime = CACurrentMediaTime() + 3;
        self.layer.add(rotation, forKey: "rotationAnimation")
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here