- 每次设定完毕 anchorPoint 之后,要重新设置一下 frame, 这样postion 才会从新进行计算
- position是layer中的anchorPoint在superLayer中的位置坐标
- 互不影响原则:单独修改position与anchorPoint中任何一个属性都不影响另一个属性。
- frame、position与anchorPoint有以下关系:
frame.origin.x = position.x - anchorPoint.x * bounds.size.width;
frame.origin.y = position.y - anchorPoint.y * bounds.size.height;
下面是一个 从右上角 -》 左下角的一个动画效果, 主要缩放的时候,最小值不能为0,否则收缩回去的时候,就看不见动画效果了
func didTap3() {
let y: Double = 82 + 140 + 80
if (isOpen2 == false) {
isOpen2 = true
self.btn2?.removeFromSuperview()
self.btn2 = UIButton()
self.btn2?.backgroundColor = UIColor.brown
self.view.addSubview(self.btn2!)
let frame = CGRect(x:235.333 ,y:y, width:168.667, height: 140)
self.btn2?.frame = frame
let view = self.btn2!
view.layer.anchorPoint = CGPoint(x: 1, y: 0)
view.frame = frame
view.layer.setAffineTransform(CGAffineTransform.init(scaleX: 0.001, y: 0.001))
UIView.animate(withDuration: 0.3, animations: {
view.layer.setAffineTransform(CGAffineTransform.init(scaleX: 1, y: 1))
}) { (finished) in
}
} else {
isOpen2 = false
let view = self.btn2!
view.layer.setAffineTransform(CGAffineTransform.init(scaleX: 1, y: 1))
UIView.animate(withDuration: 0.3, animations: {
view.layer.setAffineTransform(CGAffineTransform.init(scaleX: 0.001, y: 0.001))// 不能写0, 0时,没有动画效果了,应该是个系统bug
}) { (finished) in
}
}
}
网友评论