黄色块开始执行动画时,宽高不相等,缩放比也不相等。
以左边中间位置为基准,向右边和上下三个方向拓展放大。
![1306450-4c281c85e454fb3a.png](https://upload-images.jianshu.io/upload_images/1306450-4c281c85e454fb3a.png)
黄色块为执行动画的View
![1306450-04727c956c132cbd.png](https://upload-images.jianshu.io/upload_images/1306450-04727c956c132cbd.png)
动画转变过程
关键代码如下:
private let Width:CGFloat = 132
private let Height:CGFloat = 35
func show(inView view:UIView, x:CGFloat, y:CGFloat){
// 防止快速重复执行动画导致异常(用户疯狂点击显示/隐藏,导致动画短时间内多次执行)
self.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.layer.transform = CATransform3DIdentity
// 在设置frame之前设置anchorPoint,否则初始位置异常!
self.layer.anchorPoint = CGPoint(x: 0, y: 0.5)
self.frame = CGRect(x: x, y: y, width: Width, height: Height)
view.addSubview(self)
self.layer.transform = CATransform3DMakeScale(0.2, 0.1, 0)
self.alpha = 0
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.3, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.layer.transform = CATransform3DIdentity
self.alpha = 1
})
}
func hide(){
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.3, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.layer.transform = CATransform3DMakeScale(0.2, 0.1, 1)
self.alpha = 0
}) { (finish) in
if finish{
self.removeFromSuperview()
// 恢复形变,否则下次动画异常!
self.layer.transform = CATransform3DIdentity
}
}
}