func tapAtLabel() {
guard let naviController = navigationController,
let naviView = naviController.view else { return }
let controller = FollowUserViewController()
controller.view.translatesAutoresizingMaskIntoConstraints = false
naviView.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leftAnchor.constraint(equalTo: naviView.leftAnchor),
controller.view.rightAnchor.constraint(equalTo: naviView.rightAnchor),
controller.view.topAnchor.constraint(equalTo: naviView.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: naviView.bottomAnchor)
])
controller.view.setNeedsLayout()
controller.view.layoutIfNeeded()
naviController.addChild(controller)
controller.didMove(toParent: naviController)
}
class FollowUserViewController: UIViewController {
let followView = FollowUserView()
var constraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
let minHeight: CGFloat = 80
var height: CGFloat = UIScreen.main.bounds.height * 0.7
if FDDevice.hasTopNotch {
height += 34
}
followView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(followView)
NSLayoutConstraint.activate([
followView.leftAnchor.constraint(equalTo: view.leftAnchor),
followView.rightAnchor.constraint(equalTo: view.rightAnchor),
followView.heightAnchor.constraint(equalToConstant: height),
])
constraint = followView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: height)
constraint?.isActive = true
followView.offsetYCallBack = { [weak self] constant in
guard let `self` = self else { return }
if constant <= 0 {
self.constraint?.constant = 0
} else if constant >= height - minHeight {
self.constraint?.constant = height - minHeight
} else {
self.constraint?.constant = constant
}
}
followView.panEndCallBack = { [weak self] offsetY in
guard let `self` = self else { return }
if offsetY <= height / 2 {
self.doShowUpAnimation()
} else {
self.leave()
}
}
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
doShowUpAnimation()
}
func doShowUpAnimation() {
constraint?.constant = 0
UIView.animate(withDuration: 0.15) {
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
func leave() {
view.removeFromSuperview()
willMove(toParent: nil)
removeFromParent()
}
}
swift addchildviewcontroller
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。