gpt4 book ai didi

swift - 自定义下拉菜单动画乱七八糟?

转载 作者:行者123 更新时间:2023-11-30 10:39:30 29 4
gpt4 key购买 nike

我正在尝试使用 swift 创建自定义下拉菜单。我已经创建了菜单,并且正在尝试将其动画化。动画发生了,但是动画的效果有些乱。

请参阅此链接:https://drive.google.com/file/d/13BqdYphuCcZAoEV0jG1lyF0k43Pd2JYs/view?usp=sharing

我正在尝试获得正常的平滑下拉和上拉效果。请问有人可以建议吗?

我的代码:

var editButton: UIButton = {
let btn = UIButton()
btn.setTitle("Edit", for: .normal)
btn.backgroundColor = UIColor.appBlue.withAlphaComponent(0.5)
btn.addTarget(self, action: #selector(editTable), for: .touchUpInside)
btn.isHidden = true
return btn
}()

var createFolderButton: UIButton = {
let btn = UIButton()
btn.setTitle("Create Folder", for: .normal)
btn.backgroundColor = UIColor.appBlue.withAlphaComponent(0.5)
btn.addTarget(self, action: #selector(createFolder), for: .touchUpInside)
btn.isHidden = true
return btn
}()

var signOutButton: UIButton = {
let btn = UIButton()
btn.setTitle("Sign Out", for: .normal)
btn.backgroundColor = UIColor.appBlue.withAlphaComponent(0.5)
btn.addTarget(self, action: #selector(signOut), for: .touchUpInside)
btn.isHidden = true
return btn
}()

var allButtons: [UIButton]!





override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
createDropDownMenu()
setUpNavigationButton()
allButtons = [createFolderButton, editButton, signOutButton]
}

private func createDropDownMenu(){

// StackView
let stackView = UIStackView()
stackView.axis = .vertical
stackView.distribution = .fill
stackView.alignment = .center

stackView.addArrangedSubview(createFolderButton)
stackView.addArrangedSubview(editButton)
stackView.addArrangedSubview(signOutButton)

editButton.translatesAutoresizingMaskIntoConstraints = false
editButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
editButton.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true

createFolderButton.translatesAutoresizingMaskIntoConstraints = false
createFolderButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
createFolderButton.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true

signOutButton.translatesAutoresizingMaskIntoConstraints = false
signOutButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
signOutButton.widthAnchor.constraint(equalTo: stackView.widthAnchor).isActive = true

view.addSubview(stackView)

stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
stackView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
}

private func setUpNavigationButton(){
// NavigationBar Button
let rightButton = UIBarButtonItem.menuButton(target: self, action: #selector(expandMenu), imageName: "expand", navigationBar: navigationController!.navigationBar)
navigationController?.navigationBar.tintColor = .white
navigationItem.rightBarButtonItem = rightButton
}

@objc func expandMenu() {
print("expand menu")
for btn in allButtons!{
UIView.animate(withDuration: 2.0) {
btn.isHidden = !btn.isHidden // Give value of isHidden the opposite of what is currently is
}
}
}

最佳答案

只需使用 this pod,它消除了大部分头痛

关于swift - 自定义下拉菜单动画乱七八糟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57178736/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com