使用 swift 4.2 为 UIView 制作动画

Animate UIView using swift 4.2

有人帮我做这个动画,当我点击 X 按钮时,cardView 在卡片返回后 0.3 秒从屏幕移出,它是一个原始位置(即父视图的中心),带有淡入淡出动画.当用户单击 ✅ 按钮时,右侧同样如此,卡片应向右移动。

UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
            self.cardView.frame.origin.x =  self.cardView.frame.minX - (self.cardView.frame.width + 100)
             self.cardView.transform = CGAffineTransform(rotationAngle:-45)
        }) { (true) in
            self.cardView.center = self.view.center
            self.cardView.alpha = 0;
             self.cardView.transform = CGAffineTransform.identity

            UIView.animate(withDuration: 0.5, animations: {
                self.cardView.alpha = 1.0;
             }, completion: { (true) in

            })
        }

图片示例 -

我认为您的主要问题是您没有调用真正使动画发生的 layoutIfNeeded() 函数。检查文档。试试这个功能它应该做你想做的。

    func doAnimationAction(isTick: Bool) {

    let originToReturn = CGPoint(x: self.view.frame.origin.x, y: self.cardView.frame.origin.y)

    UIView.animate(withDuration: 0.5, animations: {
        self.cardView.alpha = 0.0
        if isTick {
            // Go left
            self.cardView.frame.origin.x =  0 - (self.cardView.frame.width * 1.5)

        } else {
            // Go right (you can change them)
            self.cardView.frame.origin.x =  self.view.frame.width + (self.cardView.frame.width * 1.5)
        }
        self.cardView.transform = CGAffineTransform(rotationAngle:-45)
        self.cardView.layoutIfNeeded()
        self.cardView.superview?.layoutIfNeeded()
    }) { (finished) in
        // Here you can update the cardViewData
        UIView.animate(withDuration: 0.5, animations: {
            self.cardView.transform = CGAffineTransform(rotationAngle:0)
            self.cardView.frame.origin =  originToReturn
            self.cardView.alpha = 1.0
            self.cardView.layoutIfNeeded()
            self.cardView.superview?.layoutIfNeeded()
        }, completion: { (finishedSecondAnimation) in

        })


    }


}