用法很简单,重新添加约束后调用layoutIfNeeded
即可
UIView.animate(withDuration: duration) {
self.layoutIfNeeded()
}
简单写个分类:
extension UIView {
enum AniationOriginPostion {
case left
case right
case top
case bottom
}
func showView(_ view: UIView ,
duration: TimeInterval = 0.3,
position:AniationOriginPostion = .right ,
percent:CGFloat = 1
)
{
self.addSubview(view)
var x:CGFloat = 0
var y:CGFloat = 0
switch position {
case .left:
x = -self.frame.size.width
case .right:
x = self.frame.size.width
case .top:
y = -self.frame.size.height
case .bottom:
y = self.frame.size.height
}
view.frame = CGRect.init(x: x, y: y, width: self.frame.size.width, height: self.frame.size.height)
view.snp.remakeConstraints { (make) in
switch position{
case .left:
make.left.top.bottom.equalTo(self)
make.width.equalTo(self).multipliedBy(percent)
case .right:
make.right.top.bottom.equalTo(self)
make.width.equalTo(self).multipliedBy(percent)
case .bottom:
make.left.right.bottom.equalTo(self)
make.height.equalTo(self).multipliedBy(percent)
case .top:
make.left.right.top.equalTo(self)
make.height.equalTo(self).multipliedBy(percent)
}
}
UIView.animate(withDuration: duration) {
self.layoutIfNeeded()
}
}
}
调用:
self.grayView.showView(view, duration: 0.3, position: .right, percent: 0.8)