class ExChangeView: UIView {
private var fromTitle = "标题1"
private lazy var fromButton: UIButton = {
let button = UIButton()
button.setTitle(fromTitle, for: .normal)
return button
}()
private var toTitle = "标题2"
private lazy var toButton: UIButton = {
let button = UIButton()
button.setTitle(toTitle, for: .normal)
return button
}()
private lazy var exchangeButton: UIButton = {
let button = UIButton()
button.setTitle("交换", for: .normal)
button.addTarget(self, action: #selector(exchangeTap), for: .touchDown)
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupLayout() {
addSubview(exchangeButton)
exchangeButton.snp.makeConstraints {
$0.size.equalTo(kSize(16))
$0.center.equalToSuperview()
}
addSubview(fromButton)
fromButton.snp.makeConstraints {
$0.centerY.equalToSuperview()
$0.right.equalTo(exchangeButton.snp.left).offset(-kSize(10))
$0.left.equalTo(kSize(10))
}
addSubview(toButton)
toButton.snp.makeConstraints {
$0.centerY.equalToSuperview()
$0.left.equalTo(exchangeButton.snp.right).offset(kSize(10))
$0.right.equalTo(-kSize(10))
}
}
@objc func exchangeTap() {
guard let fromSnap = fromButton.snapshotView(afterScreenUpdates: false), let toSnap = toButton.snapshotView(afterScreenUpdates: false) else { return }
(toTitle, fromTitle) = (fromTitle, toTitle)
fromButton.setTitle(fromTitle, for: .normal)
fromButton.alpha = 0
fromSnap.frame = fromButton.frame
addSubview(fromSnap)
toButton.setTitle(toTitle, for: .normal)
toButton.alpha = 0
toSnap.frame = toButton.frame
addSubview(toSnap)
UIView.animate(withDuration: 0.4, animations: {
(fromSnap.frame,toSnap.frame) = (toSnap.frame, fromSnap.frame)
}) { (complete) in
self.fromButton.alpha = 1
fromSnap.removeFromSuperview()
self.toButton.alpha = 1
toSnap.removeFromSuperview()
}
}
}
Swift 开发笔记5 - 交换位置的View
最后编辑于 :
?著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事?!?“怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- weiliao.app.dSYM的路径是 atos方法 生成的dSYM文件需要注意的问题 1.每次程序重新编译,都...