使用Swift 4.2对UIView进行动画处理

时间:2019-01-17 07:37:39

标签: ios iphone swift uiviewanimation

有人,请帮助我制作此动画,当我单击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

            })
        }

图片示例-

Sceenshot

1 个答案:

答案 0 :(得分:1)

我相信您的主要问题是您没有调用实际上使动画发生的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

        })


    }


}