我想更改用户登录或注销的菜单项。
我在网上搜索了很多,但没有找到一个好的解决方案。
这是我的
SettingLauncher
类返回视图和菜单项。
答:在这里,我选择要展示的物品
swiftkeychainwrapper
import UIKit
import SwiftKeychainWrapper
class SettingLauncher:NSObject,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDel egateFlowLayout{
let cellId = "cellId"
let blackView = UIView()
let cellHeight : CGFloat = 50
var homeController: HomeController?
var defaultController : DefaultController?
//var productDetailController : ProductDetailController?
override init() {
super.init()
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(SettingCell.self, forCellWithReuseIdentifier: cellId)
}
let settings : [Setting] = {
let isLoggedIn : String? = KeychainWrapper.standard.string(forKey: "myKey")
// menu items
let settingOne = Setting(name: .Profile, imageName: "profile-icon")
let settingTwo = Setting(name: .Login, imageName: "login-icon")
let settingThree = Setting(name: .Orders, imageName: "orders-icon")
let settingFour = Setting(name: .Category,imageName: "category-icon")
let settingFive = Setting(name: .Cancel, imageName: "cancel-icon")
let settingSix = Setting(name: .SignOut, imageName: "login-icon")
// A
if isLoggedIn == "true" {
return [settingOne,settingThree,settingFour,settingSix]
}
else{
return [settingTwo,settingThree,settingFour]
}
}()
let collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
//show menu
func showHambuger(){
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleCancel)))
window.addSubview(blackView)
window.addSubview(collectionView)
let height : CGFloat = CGFloat(settings.count) * cellHeight
let y = window.frame.height - height
collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)
blackView.frame = window.frame
blackView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
self.collectionView.reloadData()
}, completion: nil)
}
}
@objc func handleCancel(setting: Setting){
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = CGRect(x:0,y: window.frame.height,width: self.collectionView.frame.width,height: self.collectionView.frame.height)
}
}) { (completed: Bool) in
if setting.name != .Cancel {
//self.productDetailController?.showControllerForSetting(setting: setting)
self.defaultController?.showControllerForSetting(setting: setting)
}
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return settings.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingCell
let setting = settings[indexPath.item]
print(setting)
cell.setting = setting
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: cellHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let setting = self.settings[indexPath.item]
handleCancel(setting: setting)
}
}
在DefaultController类中,我这样调用函数
lazy var settingLauncher : SettingLauncher = {
let launcher = SettingLauncher()
launcher.defaultController = self
return launcher
}()
@objc func handleHambugerButton(){
settingLauncher.showHambuger()
}
谢谢你的帮助。